【发布时间】:2022-12-01 04:44:47
【问题描述】:
所以我是一个 python 初学者,试图抓取这个网站http://www.edwaittimes.ca/WaitTimes.aspx 这给了医院等待时间。目前我正在尝试打印所有医院的名称。
如果 .html 文件位于我正在使用的 python 文件所在的文件夹中,我的代码就可以工作
from bs4 import BeautifulSoup
import requests
def print_hospitals():
with open('website.html','r') as html_file:
content = html_file.read()
soup = BeautifulSoup(content, 'lxml')
hospital_table = soup.find_all('div',class_="Row")
for hospital in hospital_table:
if hospital.a is not None:
print(hospital.a.text)
但是当我将 requests.get 与 URL 一起使用时。该代码不打印任何内容。也没有错误消息。
from bs4 import BeautifulSoup
import requests
def print_hospitals_request():
html_text = requests.get('http://www.edwaittimes.ca/WaitTimes.aspx').text
soup = BeautifulSoup(html_text, 'lxml')
hospital_table = soup.find_all('div',class_="Row")
for hospital in hospital_table:
if hospital.a is not None:
print(hospital.a.text)
谁能帮我解决这个问题
【问题讨论】:
-
我认为您已经从浏览器复制了网页的完整代码并将其保存到您的 HTML 文件中,是吗?您正在寻找的信息由某种类型的脚本加载,并且无法从您正在抓取的链接中获得。乍一看,从这个页面中抓取数据似乎并不那么容易。
标签: python beautifulsoup python-requests-html