【问题标题】:Scrape image from amazon with python 3 and beautifulsoup使用 python 3 和 beautifulsoup 从亚马逊抓取图像
【发布时间】: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


【解决方案1】:

问题是图像是动态加载的;检查页面,感谢 BeautifulSoup documentation,给定产品,我能够抓取所有需要的图像。

获取给定链接的页面

我有一个存储数据的类,所以我将页面信息保存在实例中...

import urllib
from bs4 import BeautifulSoup

def take_page(self, url_page):
    req = urllib.request.Request(
        url_page,
        data=None
    )
    f = urllib.request.urlopen(req)
    page = f.read().decode('utf-8')
    self.page = page

抓取图片

以下简单方法将返回第一张图片,尺寸最小

import json

def take_image(self):
    soup = BeautifulSoup(self.page, 'html.parser')
    img_div = soup.find(id="imgTagWrapperId")

    imgs_str = img_div.img.get('data-a-dynamic-image')  # a string in Json format

    # convert to a dictionary
    imgs_dict = json.loads(imgs_str)
    #each key in the dictionary is a link of an image, and the value shows the size (print all the dictionay to inspect)
    num_element = 0 
    first_link = list(imgs_dict.keys())[num_element]
    return first_link

所以,您可以根据自己的需要应用这些方法,我认为这就是您改进代码所需要的全部内容。

【讨论】:

    【解决方案2】:

    查看页面上“所有”img 的代码示例

    for asin in asins:
        site = 'https://www.amazon.it/'
        start = 'dp/'
        end = '/'
        url = site + start + asin + end
        print(url)
        resp1 = requests.get(url).content
    
    
        soup = BeautifulSoup(resp1, "html.parser")
        for i in soup.find_all("img"):
            print(i)
    

    【讨论】:

      【解决方案3】:

      执行此操作的正确方法是通过 Amazon Affiliate API 帐户,但如果您没有帐户。这是最近使用 ScraperAPI lxmlcssselectPIL 的代码

      关键部分是dom.cssselect,用于从页面上的元素获取图像、请求代理和使用 PIL 正确保存图像。在书籍上测试,其他页面将使用更高的元素

      def save_img(url, name):
          response = requests.get(PROXY + url, stream=True)
          out_path = f'static/bookimg/{name}.jpg'
          try:
              i = Image.open(BytesIO(response.content))
              i.save(out_path)
          except (UnidentifiedImageError, OSError) as e:
              print(e)
      
      
      def get_img_by_asin(asin, save_name):
          url = PROXY + f'https://www.amazon.co.uk/dp/{asin}/'
          print(url)
          html = requests.get(url).content
          dom = fromstring(html)
          try:
              img = dom.cssselect("#ebooks-img-canvas img")[-1]
              save_img(img.get('src'), save_name)
          except IndexError:
              print('No image or bad response')
      

      https://gist.github.com/fmalina/03c84100e84ecc2ae2cd23d60e11959e

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-11
        • 2022-01-18
        • 2015-03-22
        • 1970-01-01
        • 2020-09-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多