【问题标题】:Merchant id not found - Amazon未找到商家 ID - 亚马逊
【发布时间】:2021-12-26 07:17:19
【问题描述】:

我无法在亚马逊产品页面上找到商家 ID,是我遗漏了什么吗?任何帮助都会很棒! 我总是在终端上收到相同的消息:“未找到商家 ID”。 网址:https://www.amazon.com/dp/B004X4KRW0/ref=olp-opf-redir?aod=1&ie=UTF8&condition=NEW&th=1 目标:使用python列出所有商家ID。 什么是商户号? 对于亚马逊上的每个卖家,商家 id 唯一地标识他们,例如从上面的网站 URL 中,如果我将亚马逊的商家 id 定位为卖家,它将在 html 中标识为 ATVPDKIKX0DER亚马逊(美国): <div id="fast-track" class="a-section a-spacing-none"> <input type="hidden" id="ftSelectAsin" value="B004X4KRW0"/> <input type="hidden" id="ftSelectMerchant" value="ATVPDKIKX0DER"/> 因此,我尝试使用 xpath 来打印所有卖家的商家 ID(输出)。

# Get Seller merchant ID
# Default Merchant ID
merchant_id = ""
# Try to find merchant ID with xpath
try:
    merchant_id = offer.xpath(
         .//input[@id='ftSelectMerchant' or @id='ddmSelectMerchant']"
    )[0].value
except IndexError:
    # try to find merchant ID with regex
    try:
        merchant_script = offer.xpath(".//script")[0].text.strip()
        find_merchant_id = re.search(
            r"merchantId = \"(\w+?)\";", merchant_script
        )
        if find_merchant_id:
            merchant_id = find_merchant_id.group(1)
    except IndexError:
        pass
log.info(f"merchant_id: {merchant_id}")
# log failure to find merchant ID
if not merchant_id:
    log.debug("No Merchant ID found")```

【问题讨论】:

  • 请提供更多信息。您需要共享最少的复制代码。目标网站网址是什么? “找不到”是什么意思?
  • 刚刚更新,如果其他详细信息有助于解释我正在尝试做的事情,请告诉我。
  • 所以打印了“No Merchant ID found”,log.info(f"merchant_id: {merchant_id}") 行打印什么?您是否使用 pass 隐藏了异常?
  • 就这个:|INFO| merchant_id: None |DEBUG| No Merchant ID found

标签: python amazon


【解决方案1】:

看来您正在抓取隐藏参数。可能有很多方法可以做到这一点。我会以两种方式展示我的工作。

这里使用硒。 element.get_attribute("innerHTML") 给出 html 字符串。只需使用正则表达式提取值。

import re

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

url = "https://www.amazon.com/dp/B004X4KRW0"

# set headless
options = Options()
options.headless = True

driver = webdriver.Firefox(options=options)
driver.get(url)

element = driver.find_element_by_xpath("//div[@class='a-section']")

innerhtml = element.get_attribute("innerHTML")

# find and get value
a = re.search('<.*merchantID.*value="(.*)"', innerhtml)

print(a.groups()[0])  # ATVPDKIKX0DER

另一种方法是使用 BeautifulSoup 和请求。这比较简单,但有时会失败(可能是服务器的响应,不确定......)

import urllib.request
from bs4 import BeautifulSoup

url = 'https://www.amazon.com/dp/B004X4KRW0'

html = urllib.request.urlopen(url).read().decode('utf-8')

soup= BeautifulSoup(html, "lxml")

value = soup.find_all("input", {"id":"merchantID"})[0]['value']

print(value)  # ATVPDKIKX0DER

(我正在使用 selenium 检查亚马逊网站以了解价格变化,并且有时属性名称会发生​​更改。因此最好不时检查一切是否正常工作。)

【讨论】:

  • 谢谢!这也有效: DEFAULT_AMAZON_MERCHANT_ID = "ATVPDKIKX0DER" try: # 这是亚马逊的优惠吗? if is_amazon_offer(offer):merchant_id = DEFAULT_AMAZON_MERCHANT_ID ]
  • 很高兴听到这个消息。如果答案有帮助,我们将不胜感激。
  • 完成!对不起,我是新人。 :)
  • 没问题!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2022-12-03
  • 2023-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多