簡體   English   中英

使用Selenium(Python)抓取網站上的所有工具提示嗎?

[英]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_dateend_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}'

或轉換日期並將其存儲為datetime對象(您可以在此處閱讀有關將時間從UTC轉換為本地時區的信息 )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM