[英]Scraping all tooltips in a website with Selenium(Python)?
我目前正在嘗試抓取此網站https://schedule.townsville-port.com.au/
我想將所有單獨的工具提示中的文字都刮掉。
這是我必須懸停的典型元素的html看起來像
<div event_id="55591" class="dhx_cal_event_line past_event" style="position:absolute; top:2px; height: 42px; left:1px; width:750px;"><div>
這是工具提示的典型html外觀
<div class="dhtmlXTooltip tooltip" style="visibility: visible; left: 803px; bottom:74px;
我嘗試了各種組合,例如嘗試直接刮取工具提示,還嘗試通過將鼠標懸停在需要懸停的位置來刮取html。
tool_tips=driver.find_elements_by_class_name("dhx_cal_event_line past_event")
tool_tips=driver.find_elements_by_xpath("//div[@class=dhx_cal_event_line past_event]")
tool_tips=driver.find_element_by_css_selector("dhx_cal_event_line past_event")
我也嘗試使用“ dhtmlXTooltip工具提示”而不是“ dhx_cal_event_line past_event”使用相同的代碼
我真的不明白為什么。
tool_tips=driver.find_elements_by_class_name("dhx_cal_event_line past_event")
不起作用
可以使用Beautifulsoup解決此問題嗎? 既然html是動態的並且正在變化?
如果您在Chrome DevTools中打開“網絡”標簽,然后按XHR進行過濾,您會看到該網站向http://schedule.townsville-port.com.au/spotschedule.php
發出了請求。
from bs4 import BeautifulSoup
import requests
url = 'http://schedule.townsville-port.com.au/spotschedule.php'
r = requests.get(url, verify=False)
soup = BeautifulSoup(r.text, 'xml')
transports = {}
events = soup.find_all('event')
for e in events:
transport_id = e['id']
transport = {child.name: child.text for child in e.children}
transports[transport_id] = transport
import pprint
pprint.pprint(transports)
輸出:
{'48165': {'IMO': '8201480',
'app_rec': 'Approved',
'cargo': 'Passenger Vessel (Import)',
'details': 'Inchcape Shipping Services Pty Limited',
'duration': '8',
'end_date': '2018-02-17 14:03:00.000',
'sectionID': '10',
'start_date': '2018-02-17 06:44:00.000',
'text': 'ARTANIA',
'visit_id': '19109'},
...
}
我發現擺脫SSLError
的唯一方法是使用verify=False
禁用證書驗證,您可以在此處了解更多信息。
注意,開始start_date
和end_date
是UTC時間,因此您可以指定timeshift
查詢參數:
import time
utc_offset = -time.localtime().tm_gmtoff // 60 # in minutes
url = f'http://schedule.townsville-port.com.au/spotschedule.php?timeshift={utc_offset}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.