gpt4 book ai didi

python - BeautifulSoup 和 Amazon.co.uk

转载 作者:行者123 更新时间:2023-11-28 19:25:55 25 4
gpt4 key购买 nike

我正在尝试解析亚马逊以编制价格列表,作为与统计相关的更大项目的一部分。但是,我很难过。我想知道是否有人可以查看我的代码并告诉我哪里出错了?

#!/usr/bin/python
# -*- coding: utf-8 -*-
import mechanize
from bs4 import BeautifulSoup

URL_00 = "http://www.amazon.co.uk/Call-Duty-Black-Ops-PS3/dp/B007WPF7FE/ref=sr_1_2?ie=UTF8&qid=1352117194&sr=8-2"

bro = mechanize.Browser()
resp = bro.open(URL_00)
html = resp.get_data()
soup_00 = BeautifulSoup(html)
price = soup_00.find('b', {'class':'priceLarge'})
print price #this should return at the very least the text enclosed in a tag

根据屏幕截图,我上面写的应该可以,不是吗?

http://i.imgur.com/bPVe1.png (cannot post an image as a newbie..)

好吧,如果我将 last 之前的行更改为:

 price = soup_00.find('b', {'class':'priceLarge'}).contents[0].string

price = soup_00.find('b', {'class':'priceLarge'}).text

我收到“noneType”错误。

我很困惑为什么会这样。 Chrome 上 URL 中的页面编码为 UTF8,我的脚本在第 2 行中调整为 UTF8。我已将其更改为 ISO(根据页面的内部 HTML),但这产生了零差异,所以我肯定编码不是这里的问题。

另外,不知道这是否相关,但我在 Linux 上的系统区域设置为 UTF-8 应该不会造成问题吧?

最佳答案

Amazon 提供了 API,因此无需执行此操作

https://affiliate-program.amazon.co.uk/gp/advertising/api/detail/main.html

The Product Advertising API helps you advertise Amazon products using product search and look up capability, product information and features such as Customer Reviews, Similar Products, Wish Lists and New and Used listings.

这里有更多详细信息:Amazon API library for Python?

我正在使用 API,它比从网页上抓取数据更容易和可靠,即使是 BS。您还将获得新品、二手等的价格列表,而不仅仅是“头条”价格。

关于python - BeautifulSoup 和 Amazon.co.uk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13231937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com