使用 Beautiful Soup 查找特定类

新手上路,请多包涵

我正在尝试使用 Beautiful Soup 从 Zillow 抓取房价数据。

我通过属性 ID 获取网页,例如。 http://www.zillow.com/homes/for_sale/18429834_zpid/

当我尝试 find_all() 函数时,我没有得到任何结果:

 results = soup.find_all('div', attrs={"class":"home-summary-row"})

但是,如果我使用 HTML 并将其削减到我想要的部分,例如:

 <html>
    <body>
        <div class=" status-icon-row for-sale-row home-summary-row">
        </div>
        <div class=" home-summary-row">
            <span class=""> $1,342,144 </span>
        </div>
    </body>
</html>

我得到 2 个结果,都 <div>home-summary-row 。所以,我的问题是,为什么在搜索整页时没有得到任何结果?


工作示例:

 from bs4 import BeautifulSoup
import requests

zpid = "18429834"
url = "http://www.zillow.com/homes/" + zpid + "_zpid/"
response = requests.get(url)
html = response.content
#html = '<html><body><div class=" status-icon-row for-sale-row home-summary-row"></div><div class=" home-summary-row"><span class=""> $1,342,144 </span></div></body></html>'
soup = BeautifulSoup(html, "html5lib")

results = soup.find_all('div', attrs={"class":"home-summary-row"})
print(results)

原文由 SFBA26 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 653
2 个回答

根据 W3.org Validator 的说法,HTML 存在许多问题,例如杂散的结束标记和跨多行拆分的标记。例如:

 <a
href="http://www.zillow.com/danville-ca-94526/sold/"  title="Recent home sales" class=""  data-za-action="Recent Home Sales"  >

这种标记会使 BeautifulSoup 更难解析 HTML。

您可能想尝试运行一些程序来清理 HTML,例如删除每行末尾的换行符和尾随空格。 BeautifulSoup 还可以为您清理 HTML 树:

 from BeautifulSoup import BeautifulSoup
tree = BeautifulSoup(bad_html)
good_html = tree.prettify()

原文由 Soviut 发布,翻译遵循 CC BY-SA 3.0 许可协议

您的 HTML 格式 正确,在这种情况下,选择正确的解析器至关重要。在 BeautifulSoup 中,目前有 3 个可用的 HTML 解析器,它们以 不同的方式工作和处理损坏的 HTML

  • html.parser (内置,无需额外模块)
  • lxml (最快,需要安装 lxml
  • html5lib (最宽松,需要安装 html5lib

解析器文档页面之间 的差异更详细地描述了这些差异。在您的情况下,为了证明差异:

 >>> from bs4 import BeautifulSoup
>>> import requests
>>>
>>> zpid = "18429834"
>>> url = "http://www.zillow.com/homes/" + zpid + "_zpid/"
>>> response = requests.get(url)
>>> html = response.content
>>>
>>> len(BeautifulSoup(html, "html5lib").find_all('div', attrs={"class":"home-summary-row"}))
0
>>> len(BeautifulSoup(html, "html.parser").find_all('div', attrs={"class":"home-summary-row"}))
3
>>> len(BeautifulSoup(html, "lxml").find_all('div', attrs={"class":"home-summary-row"}))
3

如您所见,在您的情况下, html.parserlxml 都完成了工作,但 html5lib 却没有。

原文由 alecxe 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题