使用Python查找亚马逊类别

-1 投票
1 回答
924 浏览
提问于 2025-04-18 11:07

我想获取amazon的分类信息,我打算通过爬虫的方式来实现,而不是使用API。我已经爬取了http://www.amazon.com,并获取了“按部门浏览”下的所有分类和子分类。我创建了一个网络服务来完成这个任务,代码在这里:

@route('/hello')
def hello():
    text=list();
    link=list();
    req = urllib2.Request("http://www.amazon.com",
                  headers={"Content-Type": "application/json"})
    html=urllib2.urlopen(req).read()
    soup = BeautifulSoup(html)
    last_page = soup.find('div', id="nav_subcats")
    for elm in last_page.findAll('a'):
        texts = elm.text
        links = elm.get('href')
        links = links.partition("&node=")[2]
        text.append(texts)
        link.append(links)
    alltext=list();
    for i,j in zip(text,link):
        alltext.append({"name":i,"id":j})
    response.content_type = 'application/json'
    print(alltext)
    return dumps(alltext)
run(host='localhost', port=8080, debug=True)

我将分类名称和分类ID作为一个JSON对象传递给我的一个成员,然后再把它传递给API,以获取每个分类的产品列表。

这段代码是用JAVA写的,代码如下:

for (int pageno = 1; pageno <= 10; pageno++) {
            String page = String.valueOf(pageno);
            String category_string = selectedOption.get("category_name").toString();
            String category_id = selectedOption.get("category_id").toString();
            final Map<String, String> params = new HashMap<String, String>(3);
            params.put(AmazonClient.Op.PARAM_OPERATION, "ItemSearch");
            params.put("SearchIndex", category_string);
            params.put("BrowseNodeId", category_id);
            params.put("Keywords", category_string);
            params.put("ItemPage", page);
            System.out.println(client.documentToString(client.getXml(params)));
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            Document doc = null;
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputStream is = client.getInputStream(params);

            doc = db.parse(is);
            NodeList itemList = doc.getElementsByTagName("Items");

但是当我把分类ID作为BrowseNodeId,分类名称作为关键词和搜索索引传递时,出现了这个错误:

 For example
     Search Index and Keyword -Amazon Instant Video
     BrowseNodeId-2858778011

The value you specified for SearchIndex is invalid. Valid values include [ 'All','Apparel',...................................reless','WirelessAccessories' ].

我想知道从哪个amazon的网址可以获取到所有的分类及其浏览节点。

谢谢!

1 个回答

0

我之前从来没有看过亚马逊的API,所以这只是我的猜测。不过,根据错误信息来看,“亚马逊即时视频”似乎不是一个有效的搜索索引。虽然它在下拉列表中出现,但这并不一定意味着它是有效的搜索索引。

这是美国的搜索索引列表:http://docs.aws.amazon.com/AWSECommerceService/latest/DG/USSearchIndexParamForItemsearch.html。我不知道这个列表更新到什么程度,但“亚马逊即时视频”并没有出现在这个列表上。错误信息中包含了一些有效的搜索索引值,这些值似乎和上面的列表对应。

如果你想查看其他地区的索引,可以去这里:http://docs.aws.amazon.com/AWSECommerceService/latest/DG/APPNDX_SearchIndexParamForItemsearch.html

我觉得这并不是一个编码问题。

你可以看看python-amazon-product-api。这个API可能对你有帮助,文档也可能给你一些灵感。

撰写回答