【发布时间】:2019-12-07 04:59:18
【问题描述】:
我需要从亚马逊的产品页面中抓取主图像。 我将 ASIN 存储到一个列表中,并使用 for 循环构建每个产品页面。 我正在尝试抓取图像,但我不能。我尝试使用此代码:
#declare a session object
session = HTMLSession()
#ignore warnings
if not sys.warnoptions:
warnings.simplefilter("ignore")
urls = ['https://www.amazon.it/gp/bestsellers/apparel/', 'https://www.amazon.it/gp/bestsellers/electronics/', 'https://www.amazon.it/gp/bestsellers/books/']
asins = []
for url in urls:
content = requests.get(url).content
decoded_content = content.decode()
asins = re.findall(r'/[^/]+/dp/([^\"?]+)', decoded_content)
#The ASIN Number will be between the dp/ and another /
for asin in asins:
site = 'https://www.amazon.it/'
start = 'dp/'
end = '/'
url = site + start + asin + end
resp1 = requests.get(url).content
soup = bsoup(resp1, "html.parser")
body = soup.find("body")
imgtag = soup.find("img", {"id":"landingImage"})
imageurl = dict(imgtag.attrs)["src"]
resp2 = request.urlopen(imaegurl)
【问题讨论】:
-
当我转到 findall (amazon.it/dp/8891822582) 收集的第一页时,我没有看到任何landingImage、id'd 项目。你在找这张照片吗? link。我看到的标签是:class="a-dynamic-image image-stretch-vertical frontImage" id = "imgBlkFront"。我可以通过 img 项目在 find_all 循环中看到它。可能最好收集到一个列表中并再次使用 re 进行修剪。它会更慢,但更稳定,因为 Amz 不喜欢抓取。
-
是的,那是我正在搜索的图像。但是你是怎么做的?可以发一下代码吗?
-
但是我总是得到相同的图像,这可能吗?
标签: python web-scraping amazon