- Python Web Scraping Cookbook
- Michael Heydt
- 189字
- 2021-06-30 18:43:59
How to do it...
The script for this recipe is 01/04_events_with_selenium.py.
- The following is the code:
from selenium import webdriver
def get_upcoming_events(url):
driver = webdriver.Firefox()
driver.get(url)
events = driver.find_elements_by_xpath('//ul[contains(@class, "list-recent-events")]/li')
for event in events:
event_details = dict()
event_details['name'] = event.find_element_by_xpath('h3[@class="event-title"]/a').text
event_details['location'] = event.find_element_by_xpath('p/span[@class="event-location"]').text
event_details['time'] = event.find_element_by_xpath('p/time').text
print(event_details)
driver.close()
get_upcoming_events('https://www.python.org/events/python-events/')
- And run the script with Python. You will see familiar output:
~ $ python 04_events_with_selenium.py
{'name': 'PyCascades 2018', 'location': 'Granville Island Stage, 1585 Johnston St, Vancouver, BC V6H 3R9, Canada', 'time': '22 Jan. – 24 Jan.'}
{'name': 'PyCon Cameroon 2018', 'location': 'Limbe, Cameroon', 'time': '24 Jan. – 29 Jan.'}
{'name': 'FOSDEM 2018', 'location': 'ULB Campus du Solbosch, Av. F. D. Roosevelt 50, 1050 Bruxelles, Belgium', 'time': '03 Feb. – 05 Feb.'}
{'name': 'PyCon Pune 2018', 'location': 'Pune, India', 'time': '08 Feb. – 12 Feb.'}
{'name': 'PyCon Colombia 2018', 'location': 'Medellin, Colombia', 'time': '09 Feb. – 12 Feb.'}
{'name': 'PyTennessee 2018', 'location': 'Nashville, TN, USA', 'time': '10 Feb. – 12 Feb.'}
During this process, Firefox will pop up and open the page. We have reused the previous recipe and adopted Selenium.

The Window Popped up by Firefox
推薦閱讀
- Truffle Quick Start Guide
- Hands-On Industrial Internet of Things
- 5G承載網(wǎng)網(wǎng)絡(luò)規(guī)劃與組網(wǎng)設(shè)計
- Spring Cloud微服務(wù)架構(gòu)進(jìn)階
- NB-IoT物聯(lián)網(wǎng)技術(shù)解析與案例詳解
- 中國互聯(lián)網(wǎng)發(fā)展報告2018
- Learning Swift(Second Edition)
- IPv6網(wǎng)絡(luò)切片:使能千行百業(yè)新體驗
- 面向5G-Advanced的關(guān)鍵技術(shù)
- 通信十年:擁抱互聯(lián)網(wǎng)
- 網(wǎng)絡(luò)工程實(shí)施技術(shù)與方案大全
- 現(xiàn)場綜合化網(wǎng)絡(luò)運(yùn)營與維護(hù):運(yùn)營商數(shù)字化轉(zhuǎn)型技術(shù)與實(shí)踐
- 網(wǎng)絡(luò)信息安全工程技術(shù)與應(yīng)用分析
- 通信系統(tǒng)實(shí)戰(zhàn)筆記:無處不在的信號處理
- 互聯(lián)網(wǎng)戰(zhàn)略變革與未來