python请求在抓取.htm页面时仅返回空集

标签 python html web-scraping beautifulsoup python-requests

我正在尝试抓取 .htm链接并且无法让我的脚本返回除 '[]' 之外的任何内容。

链接= https://www.forwardhealth.wi.gov/WIPortal/StaticContent/Member/caseloads/481-caseload.htm

import requests
from bs4 import BeautifulSoup as bs

link = 'https://www.forwardhealth.wi.gov/WIPortal/StaticContent/Member/caseloads/481-caseload.htm'
headers = {'User-Agent':'Mozilla/5.0'}
r = requests.get(link, headers=headers)
soup = bs(r.text, 'lxml')      #I've tried other html parsers in here as well as r.content

我认为问题在于我尝试与页面交互(可能是编码不正确?)。上述格式是我过去设置任何网络抓取的方式,并且没有遇到任何我无法解决的问题。最引人注目的是当我调用r.content时或r.text我收到的回复似乎很陌生:

'<HTML>\r\n<HEAD>\r\n<TITLE>481-caseload</TITLE>\r\n<META NAME="GENERATOR" CONTENT="Microsoft FrontPage 5.0">\r\n<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">\r\n</HEAD>\r\n\r\n<FRAMESET ROWS="*,48" FRAMESPACING="0" FRAMEBORDER="no" BORDER="0">\r\n<FRAME NAME="ReportArea" SRC="481-caseload/by_county_tribe/by_county_tribe.htm"\r\n MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="0" NORESIZE>\r\n<FRAMESET COLS="*" FRAMESPACING="0" FRAMEBORDER="0" BORDER="0">\r\n<FRAME NAME="ReportLinks" SRC="481-caseload/DocLinks.htm" FRAMEBORDER="0" MARGINWIDTH="2" MARGINHEIGHT="3" scrolling="auto">\r\n</FRAMESET></FRAMESET></HTML>'

这让我觉得我的脚本没有正确编写来处理上面的任何内容。我以前从未见过“Microsoft FrontPage 5.0”,并且不知道这是否是导致我的代码出错的原因。我尝试通过更改 r.encoding = #encoding here 来强制编码。任何指导都会有所帮助。

最佳答案

这是因为页面由多个嵌套iframe组成s - 基本上,当加载主“容器”页面时,浏览器会加载具有自己 URL 的单独页面。使用浏览器开发人员工具检查页面并查看您所需的内容位于哪个 iframe 中。

本页主要内容来自this url :

In [1]: import requests

In [2]: from bs4 import BeautifulSoup

In [3]: url = "https://www.forwardhealth.wi.gov/WIPortal/StaticContent/Member/caseloads/481-caseload/by_county_tribe/0.htm"

In [4]: response = requests.get(url)

In [5]: soup = BeautifulSoup(response.content, "lxml")

In [6]: soup.select_one("td.s2").get_text()
Out[6]: 'Wisconsin Medicaid'

关于python请求在抓取.htm页面时仅返回空集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47780198/

相关文章:

python - 如何查找一维张量中的重复元素

html - 如何使用 javascript 将值从一个 HTML 页面传递到另一个 HTML 页面?

html - 如何让每个单词的第一个字母大小不同,其余的大小相同

java - 如何在通过 java 抓取网页时避免 td 标签中的 HTML 空格

python - 值错误 : wrapper loop when unwrapping

python - 在python中覆盖类变量?

python - 如何使用 python 3 将特定的 json 插入到 sqlite 数据库中

javascript - 如何在 jQuery 中自动选择单选按钮

php - 如何在 html 文件中搜索简单字符串?

python - 使用 Scrapy 从文本文件中的多个 URL 中抓取所有外部链接