在 Python 中抓取第二页会给出第一页的数据



在Python中抓取第二页会得到第一页的数据。这是代码的一部分:

browser.get("https://XXXXXXXXX/0_9b34?P=2")
innerHTML = browser.execute_script("return document.body.innerHTML")      #type = str    #returns the inner HTML as a string
Eroom_M7_htmlpage = innerHTML
soup = BeautifulSoup(Eroom_M7_htmlpage, 'html.parser')      #type = bs4.BeautifulSoup
htmlprettified = soup.prettify()                            #type = str
project_items = soup.find_all('td', attrs={'headers' : 'ID Item'}) 

如果答案对初学者友好,我将不胜感激,因为我只是一个 3 个月的 Python 自学者。请问我真的需要帮助来完成我的项目:(ps :我看到了两篇关于这个的文章,但没有帮助/理解。

innerHTML = browser.execute_script("return document.body.innerHTML")      #type = str    #returns the inner HTML as a string
Eroom_M7_htmlpage = innerHTML

你应该返回page_source而不是javascript响应

.page_source是要使用的方法。

所以执行任何你想要的JavaScript,然后捕获HTML

Eroom_M7_htmlpage = browser.page_source

而不是innerhtml文档--->这里

硒使用的一个基本例子。

from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://python.org')
html = driver.page_source
print(html)

它将输出存储在变量中的网页源 .html。

最新更新