美丽的团队和亚马逊公司.u

2024-05-29 03:34:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图分析亚马逊,以编制一份价格清单,作为与统计相关的更大项目的一部分。但是,我被难住了。我想知道是否有人可以检查我的代码并告诉我哪里出错了?在

#!/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..)

如果我把最后一行改成这样的话,打印出来的结果是“[]”:

^{pr2}$

或者

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

我得到一个“非类型”错误。在

我很困惑为什么会发生这种事。chrome上URL中的页面编码是UTF8,我的脚本在第#2行中调整为UTF8。 我已经把它改成了ISO(根据页面的内部HTML),但这没有什么区别,所以我是肯定的编码不是这里的问题。在

另外,我不知道这是否相关,但是我在linux上的系统语言环境是UTF-8应该不会引起问题吧?在

欢迎有任何想法。在


Tags: theimporturlhtmlutf8findrespprice
1条回答
网友
1楼 · 发布于 2024-05-29 03:34:32

没有必要这样做,因为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。另一方面,你也不会得到一个新的价格表等标题。在

相关问题 更多 >

    热门问题