Skip to main content
 首页 » 编程设计

python之Selenium Web 在动态内容和隐藏数据表上使用 Beautiful Soup 进行抓取

2025年01月19日37傻小

真的需要这个社区的帮助!

我正在使用 Selenium 和 Beautiful Soup 在 Python 中对动态内容进行网络抓取。 问题是定价数据表无法解析为 Python,即使使用以下代码也是如此:

html=browser.execute_script('return document.body.innerHTML') 
sel_soup=BeautifulSoup(html, 'html.parser')   

但是,后来我发现,如果我在使用上面的代码之前点击网页上的“查看所有价格”按钮,我可以将那个数据表解析成python。

我的问题是如何在不使用 Selenium 单击所有“查看所有价格”按钮的情况下解析和访问我的 python 中那些隐藏的动态 td 标签信息,因为有这么多按钮。

我正在执行 Web Scraping 的网站的 url 是 https://www.cruisecritic.com/cruiseto/cruiseitineraries.cfm?port=122 , 附图是我需要的动态数据表的html。 enter image description here

非常感谢这个社区的帮助!

请您参考如下方法:

您应该在加载后定位元素并获取 arguments[0] 而不是通过 document 获取整个页面

html_of_interest=driver.execute_script('return arguments[0].innerHTML',element) 
sel_soup=BeautifulSoup(html_of_interest, 'html.parser') 

这有两个实际案例:

1

该元素尚未加载到 DOM 中,您需要等待该元素:

browser.get("url") 
sleep(experimental) # usually get will finish only after the page is loaded but sometimes there is some JS woo running after on load time 
 
try: 
    element= WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'your_id_of_interest'))) 
    print "element is ready do the thing!" 
    html_of_interest=driver.execute_script('return arguments[0].innerHTML',element) 
    sel_soup=BeautifulSoup(html_of_interest, 'html.parser') 
except TimeoutException: 
    print "Somethings wrong!"    

2

该元素在影子根中,您需要先扩展影子根,可能不是您的情况,但我会在这里提及,因为它与以后的引用有关。例如:

import selenium 
from selenium import webdriver 
driver = webdriver.Chrome() 
from bs4 import BeautifulSoup 
 
 
def expand_shadow_element(element): 
  shadow_root = driver.execute_script('return arguments[0].shadowRoot', element) 
  return shadow_root 
 
driver.get("chrome://settings") 
root1 = driver.find_element_by_tag_name('settings-ui') 
 
html_of_interest=driver.execute_script('return arguments[0].innerHTML',root1) 
sel_soup=BeautifulSoup(html_of_interest, 'html.parser') 
sel_soup# empty root not expande 
 
shadow_root1 = expand_shadow_element(root1) 
 
html_of_interest=driver.execute_script('return arguments[0].innerHTML',shadow_root1) 
sel_soup=BeautifulSoup(html_of_interest, 'html.parser') 
sel_soup