【问题标题】:amazon price web scraping problem with python, requests and bs4亚马逊价格网络抓取问题与 python、请求和 bs4
【发布时间】:2019-09-04 21:32:54
【问题描述】:

因此,我编写了一段代码,用于获取有关亚马逊产品的信息,并且可以获取价格并设置条件。如果满足价格条件,我将使用 gmail 发送消息到我自己。问题是当我使用代码得到它说的价格时

'NoneType' object has no attribute 'get_text'

这是我的代码。不是它的全部部分,而只是收集有关产品的信息:

url="https://www.amazon.de/Sony-DigitalKamera-Touch-Display-Vollformatsensor-KartenSlots/dp/B07B4L1PQ8/ref=sr_1_3?keywords=sony+a7&qid=1561393494&s=gateway&sr=8-3"
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"}
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()
converted_price=float(price[0:6])
print(converted_price)
print(title.strip())

【问题讨论】:

  • 你能检查soup的内容来验证它是你期望的html吗?
  • 是的,我检查了它是我期待的 html
  • edit您的问题并将错误的填充回溯添加到它。
  • 根据您的错误,这意味着soup 无法找到 id 等于“priceblock_ourprice”的元素

标签: python beautifulsoup


【解决方案1】:

我在您正在抓取的页面中找不到priceblock_ourprice

from bs4 import BeautifulSoup
import requests

url = "https://www.amazon.de/Sony-DigitalKamera-Touch-Display-Vollformatsensor-KartenSlots/dp/B07B4L1PQ8/ref=sr_1_3?keywords=sony+a7&qid=1561393494&s=gateway&sr=8-3"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find('span', {'id': 'productTitle'}).text
print(title.strip())

【讨论】:

    【解决方案2】:

    只需将用户代理更改为 Mozilla only 并尝试以下代码。

    url="https://www.amazon.de/Sony-DigitalKamera-Touch-Display-Vollformatsensor-KartenSlots/dp/B07B4L1PQ8/ref=sr_1_3?keywords=sony+a7&qid=1561393494&s=gateway&sr=8-3"
    headers={"User-Agent":"Mozilla/5.0"}
    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()
    price=price.replace(',','')
    converted_price=float(price[0:6])
    print(converted_price)
    print(title.strip())
    

    输出:

    1.9213
    Sony Alpha 7M3 E-Mount Vollformat Digitalkamera ILCE-7M3 (24,2 Megapixel, 7,6cm (3 Zoll) Touch-Display, Exmor R CMOS Vollformatsensor, XGA OLED Sucher, 2 Kartenslots, nur Gehäuse) schwarz
    

    【讨论】:

    • 我应该发布整个代码,也许这是另一个问题
    • 但是我刚刚在我的笔记本电脑上运行,它返回了我上面的输出。检查您是否被防火墙或其他东西阻止。
    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 2015-11-03
    • 1970-01-01
    相关资源
    最近更新 更多