官术网_书友最值得收藏!

  • 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.

  1. 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/')
  1. 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
主站蜘蛛池模板: 道孚县| 肥东县| 中江县| 稻城县| 化德县| 滨海县| 吉水县| 曲靖市| 蕉岭县| 嘉义市| 克拉玛依市| 甘肃省| 赣州市| 吉木萨尔县| 天镇县| 佳木斯市| 抚顺市| 通州市| 韶关市| 湖州市| 陕西省| 延寿县| 安丘市| 麻江县| 财经| 顺平县| 敦化市| 阜南县| 青阳县| 页游| 庄浪县| 蓝田县| 双牌县| 简阳市| 阳新县| 葫芦岛市| 石景山区| 海丰县| 河西区| 新乡县| 兴仁县|