Python Web Scraping: Display the date, days, title, city, country of next 25 Hackevents
Python Web Scraping: Exercise-22 with Solution
Write a Python program to display the date, days, title, city, country of next 25 Hackevents.
Sample Solution:
Python Code:
import requests
from bs4 import BeautifulSoup
res = requests.get('https://hackevents.co/hackathons')
bs = BeautifulSoup(res.text, 'lxml')
hacks_data = bs.find_all('div',{'class':'hackathon '})
for i,f in enumerate(hacks_data,1):
hacks_month = f.find('div',{'class':'date'}).find('div',{'class':'date-month'}).text.strip()
hacks_date = f.find('div',{'class':'date'}).find('div',{'class':'date-day-number'}).text.strip()
hacks_days = f.find('div',{'class':'date'}).find('div',{'class':'date-week-days'}).text.strip()
hacks_final_date = "{} {}, {} ".format(hacks_date, hacks_month, hacks_days )
hacks_name = f.find('div',{'class':'info'}).find('h2').text.strip()
hacks_city = f.find('div',{'class':'info'}).find('p').find('span',{'class':'city'}).text.strip()
hacks_country = f.find('div',{'class':'info'}).find('p').find('span',{'class':'country'}).text.strip()
print("{:<5}{:<15}: {:<90}: {}, {}\n ".format(str(i)+')',hacks_final_date, hacks_name.title(), hacks_city, hacks_country))
Sample Output:
1) 7 Jul, Sat-Sun : Angelhack Cyprus Hackathon 2018 : Limassol, Cyprus 2) 10 Jul, Tue : Network Resilience Hackathon : Leeds, United Kingdom 3) 10 Jul, Tue : Découvrir Le Hacking Silver Camp : Strasbourg, France 4) 10 Jul, Tue-Wed : Hack4Tk Thyssenkrupp Hackathon 2018 : Essen, Germany 5) 10 Jul, Tue : Hackathon For Supply Chain Transparency With Zalando Se And Volkswagen Group : Berlin, Germany 6) 11 Jul, Wed-Fri : Clean Water Hackathon : Kensington, Australia 7) 11 Jul, Wed : Hack|Bay 2018 Evening Gala : Nürnberg, Germany 8) 11 Jul, Wed : Society Of Medical Innovation: Health Hackathon : Kensington, Australia 9) 12 Jul, Thu-Thu : Atlantic Lottery Hackathon 007: Space Lottery : Halifax, Canada 10) 12 Jul, Thu : Vodw Brussels - Digital Design Hackaton : Brussel, Belgium 11) 12 Jul, Thu : Düsseldorf: Dci Hackathon : Düsseldorf, Germany 12) 12 Jul, Thu-Fri : Aasqa Software Testing Hackathon : Bentley, Australia 13) 12 Jul, Thu-Fri : Voice Assistants For All Hackathon : Los Angeles, USA 14) 12 Jul, Thu : Crowdfunding Hackathon Barcelona : Barcelona, Spain 15) 12 Jul, Thu-Sat : Cross-Industry Ai Hack Powered By Bmw & Siemens : Munich, Germany 16) 13 Jul, Fri : Lead4Hope - Hackney For Social Change : Shoreditch, United Kingdom 17) 13 Jul, Fri-Sun : Code For Cognitive Aid (Hackathon/Makeathon/Stem) : Portola Valley, USA 18) 13 Jul, Fri : Lead4Hope - Hackney For Social Change : London, United Kingdom 19) 13 Jul, Fri-Sun : Hack For Venezuela Hackathon : New York, USA 20) 13 Jul, Fri-Sun : 'Dynamite' Hackathon : Fortitude Valley, Australia 21) 13 Jul, Fri-Sun : Blockchain Hackathon : Zürich, Switzerland 22) 13 Jul, Fri-Sat : Codefest Nigeria Hackathon (Abuja) : ABUJA, Nigeria 23) 13 Jul, Fri-Sun : Hack The Future: Between Earth & Sky : Choctaw, USA 24) 13 Jul, Fri-Sat : Pride Hacks/Axiona - Hackerfest : Montréal, Canada 25) 13 Jul, Fri-Sun : Code For Cognitive Aid : Portola Valley, United States
Flowchart:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to find the live weather report (temperature, wind speed, description and weather) of a given city.
Next: Write a Python program to download IMDB's Top 250 data (movie name, Initial release, director name and stars).
What is the difficulty level of this exercise?
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework