python - 为什么这段代码中http-response的html文件不完整?
问题描述
我正在尝试通过使用 python 和模块“requests”和“BeautifulSoup”从网站(https://www.evaschulze-aufgabenpool.de/index.php/s/smwP6ygck2SXRtF)获取一些数据,但似乎我得到一个不完整的 html 文件作为响应。例如,当使用浏览器检查时,与原始 html 文件相比,作为响应,我得到的 html 文件中的 table 标记缺少行。所以我的问题是:这是什么原因,我该如何解决这个问题?
这是我用来获取表格标签内数据的代码:
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)
解决方案
怎么了?
表格内容是动态生成的,不包含在您的请求响应中。您必须等到页面/内容加载完毕。
你可以做的是使用硒
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()
推荐阅读
- java - 如何从 Firebase 播放 mp3?
- angular - 使用可观察的更改调用管道
- macos - VirtualBox - Mac OSX 主机上的 Lubuntu 18.10 没有声音
- autodesk-forge - 为什么在尝试使用 Design Automation API 发布新活动时出现内部服务器错误?
- django - Django JSONField 动态包含和键查找
- project-reactor - Reactor StepVerifier 测试因 blockFirst() 而失败
- python - 在一个时间间隔内运行一些指令集
- c++ - C2440:“=”:无法从“const char [9]”转换为“char*”
- unicode - VB 6中的打印机对象不返回名称中带有特殊字符的打印机
- sql - 在查询中使用 where IN 子句的参数不匹配