python - 在 Python 中使用 Beautiful Soup 在线检查产品的可用性

标签 python beautifulsoup

我使用的是 python 2.7 和 Beautiful Soup 版本 4.5.1

我束手无策,试图让这个非常简单的脚本发挥作用。我的目标是通过解析产品页面的 html 并提取

中的信息,从 Best Buy 的网站获取有关 NES 控制台在线可用性状态的信息
<div class="status online-availability-status">             Sold out online     </div>

这是我第一次使用 Beautiful Soup 模块,所以如果我错过了一些明显的东西,请原谅我。这是我为尝试获取上述信息而编写的脚本:

import requests
from bs4 import BeautifulSoup

page = requests.get('http://www.bestbuy.ca/en-CA/product/nintendo-nintendo-entertainment-system-nes-classic-edition-console-clvsnesa/10488665.aspx?path=922de2a5ceb066b0f058cc567ad3d547en02')

soup = BeautifulSoup(page.content, 'html.parser')

avail = soup.findAll('div', {"class": "status online-availability-status"})

但随后我只得到一个空的 avail 列表。知道为什么吗?

非常感谢任何帮助。

最佳答案

正如上面的评论所暗示的,您似乎正在寻找一个由 JavaScript 客户端生成的标签;它在加载的页面上使用“检查”显示,但在查看页面源时则不显示,这就是对请求的调用所撤回的内容。您可以尝试使用 dryscrape(您可能需要使用 pip install dryscrape 安装)。

import dryscrape
from bs4 import BeautifulSoup
session = dryscrape.Session()
url = 'http://www.bestbuy.ca/en-CA/product/nintendo-nintendo-entertainment-system-nes-classic-edition-console-clvsnesa/10488665.aspx?path=922de2a5ceb066b0f058cc567ad3d547en02'
session.visit(url)
response = session.body()
soup = BeautifulSoup(response)
avail = soup.findAll('div', {"class": "status online-availability-status"})

这是与抓取动态生成的内容相关的问题中最流行的解决方案:

Web-scraping JavaScript page with Python

关于python - 在 Python 中使用 Beautiful Soup 在线检查产品的可用性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41373578/

相关文章:

javascript - 属性错误 : 'WebDriver' object has no attribute 'manage' 的问题

python - _sql_constraints 和 _constraints 在 OpenERP/Odoo 上的区别?

python - Beautiful Soup 为特定的 div 找到 child

python - 使用 Python 将 url 作为文本获取时出现关键错误

python - 合并到一个文件时文件数据会成倍增加,为什么?

python - 我如何在 python 中将日期转换为时间格式?

python - 作为开发的一部分,我致力于 github 并在其他地方执行。感觉不对

javascript - 使用 BeautifulSoup 抓取 JavaScript (ReactTable)

python - 无法通过 python 网络抓取从 HTML 文件中提取#document

python - 请求在表单上发布不返回生成的页面