为什么这段代码中的httpresponse的html文件不完整?

2024-09-29 23:20:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过使用python和模块“requests”和“BeautifulSoup”从一个网站(https://www.evaschulze-aufgabenpool.de/index.php/s/smwP6ygck2SXRtF)获取一些数据,但似乎得到了一个不完整的html文件作为响应。例如,当我使用浏览器检查html文件时,与原始html文件相比,我用代码得到的html文件中的表标记缺少行。所以我的问题是:这是什么原因?我如何解决这个问题

下面是我用来获取表标记内数据的代码:

import requests
from bs4 import BeautifulSoup

source = requests.get("https://www.evaschulze-aufgabenpool.de/index.php/s/smwP6ygck2SXRtF").text
soup = BeautifulSoup(source, "html.parser")
for table in soup.find_all("table"):
    print(table)

Tags: 文件数据代码httpsindexhtmlwwwtable
1条回答
网友
1楼 · 发布于 2024-09-29 23:20:42

发生了什么事?

表的内容是动态生成的,不包括在请求的响应中。您必须等待页面/内容加载

你能做的就是使用硒

from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep

url = "https://www.evaschulze-aufgabenpool.de/index.php/s/smwP6ygck2SXRtF"

driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')

driver.get(url)
#driver.implicitly_wait(10) 
sleep(3)
soup = BeautifulSoup(driver.page_source,"lxml")

for table in soup.find_all("table"):
    print(table)

driver.close()

相关问题 更多 >

    热门问题