python - 此代码用于使用 python 返回 None 的 Web Scraping。为什么?任何帮助,将不胜感激
问题描述
from bs4 import BeautifulSoup
import requests
headers = {'Use-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/79.0.3945.130 Safari/537.36'}
url = 'https://www.amazon.com/Sony-Alpha-a6400-Mirrorless-Camera/dp/B07MV3P7M8/ref=sr_1_4?keywords=sony+alpha&qid=1581656953&s=electronics&sr=1-4'
page = requests.get(url,headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id="productTitle").get_text()
price = soup.find(id="priceblock_ourprice").get_text()
print(title)
print(price)
解决方案
您的代码工作正常,但在产品页面之前有一个机器人检查,因此您的请求在该机器人检查页面中查找 span 标签,失败并返回None
。
这是一个可以帮助您的链接:python requests & beautifulsoup bot detection
推荐阅读
- .net - 在 T-SQL 中,哪个错误严重级别(如果有)允许语句继续,但导致 SqlClient 库显示一条消息
- c# - ZXing.Net从0.16.5升级到0.16.6后读取条码的标准方式
- python - 组合对象数据类型的行和列而不丢失数据
- ios - 使用 OnPlatform 更改 IOS 和 macOS 的按钮宽度
- kotlin - 您可以在不可为空的属性上使用客户吸气剂吗?
- angular - Node Red 有一个 UiBuilder,但我想从另一个应用程序使用 socket.io-client 收听它,但无法 est 连接
- c# - 将 C# 数据集批量插入 AWS PostgreSql
- xaml - TabbedPage 选项卡图标不适用于字体真棒
- java - SpringBoot中如何使用JNDI配置多个数据源?
- c++ - 预处理器错误:cblas.h:没有这样的文件或目录