import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.2)\
AppleWebKit/537.36 (KHTML, like Gecko)\
Chrome/63.0.3239.84 Safari/537.36',
'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'}
url = "https://www.python.org/events/python-events"
def upcoming_event(headers, url):
content = requests.get(url, headers=headers).content
page = BeautifulSoup(content, "lxml")
for event in page.findAll('h3'):
print("Событие:", event.text)
for date in page.findAll('time'):
print("Дата:", date.text)
for location in page.findAll('span'):
print("Место:", location.text)
upcoming_event(headers, url)
Событие:
Дата:
Место:
Событие:
Дата:
Место:
.....
from pprint import pprint
from urllib import request
from urllib.error import URLError
from bs4 import BeautifulSoup
def get_html_from_url(target_url):
try:
html = request.urlopen(target_url).read().decode('UTF-8')
return html
except URLError:
print('Не могу открыть url')
def parse_events(html_events):
events = list()
for item in html_events:
events.append({
'Событие': item.h3.a.string,
'Дата': item.p.time['datetime'],
'Место': item.find('span', {'class': 'event-location'}).string
})
return events
if __name__ == '__main__':
url = "https://www.python.org/events/python-events"
raw_html = get_html_from_url(url)
soup = BeautifulSoup(raw_html, 'html.parser')
item_list = soup.find('ul', attrs={'class': 'list-recent-events menu'})
python_events = parse_events(item_list.find_all('li'))
pprint(python_events)