首页 > 解决方案 > 此代码用于使用 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)

标签: pythonweb-scrapingbeautifulsoup

解决方案


您的代码工作正常,但在产品页面之前有一个机器人检查,因此您的请求在该机器人检查页面中查找 span 标签,失败并返回None

这是一个可以帮助您的链接:python requests & beautifulsoup bot detection


推荐阅读