【问题标题】:how to scrape product gallery image link on BeautifulSoup如何在 BeautifulSoup 上抓取产品图库图片链接
【发布时间】:2020-03-20 19:42:58
【问题描述】:

我正在尝试使用 BeautifulSoup4 https://www.daraz.com.bd/products/awei-y336-portable-wireless-bluetooth-mini-speaker-charging-cable-i128428097-s1048302559.html?search=1 从该网站上抓取产品图库图片 url,但我找不到任何方法。有些产品有 3 张图片,有些产品有更多。

谁能告诉我如何使用 BeautifulSoup 抓取画廊图片的网址?

这是我的代码

from selenium import webdriver
from bs4 import BeautifulSoup
browser = webdriver.Chrome()
browser.get("https://www.daraz.com.bd/products/awei-y336-portable-wireless-bluetooth-mini-speaker-charging-cable-i128428097-s1048302559.html?search=1")

soup = BeautifulSoup(browser.page_source, "html.parser")
container = soup.find_all("body")
for items in container:
        title = items.find("div",{"class":"pdp-product-title"}).text
        price = items.find('span', attrs={'class':'pdp-price'}).text.strip(" ৳")
        print(title,price)
browser.quit()

【问题讨论】:

  • 你做了什么?这不是软件服务。
  • Juan 我已经构建了可以从该页面提取产品标题、描述和其他信息的代码,但我不知道如何在 beautifulsoup 上抓取产品图库图片 url,因此我将其发布在这里以获取帮助。我是刚开始学习代码的新手。
  • 如果您不提供最小可重现示例,则很难提供帮助。这样人们就不必从零开始,他们可以重用你的代码。 stackoverflow.com/help/minimal-reproducible-example
  • 请参阅How to Askhelp center。这至少是您关于抓取这个确切网站的第三个问题。 Stack Overflow 不是免费的代码编写服务,也不是提供个性化指南和教程。 请参阅:How to Askhelp centermeta.stackoverflow.com/questions/261592/…
  • Juan Javier Santos Ochoa 我更新了我的帖子并添加了我的代码,请参阅

标签: python image image-processing web-scraping beautifulsoup


【解决方案1】:

嗯,这个页面很难抓取,因为它们包含您想要嵌入到大量 (JavaScript) 代码中的信息。好处是图像很容易识别,因为它们有一个模式:https://static-01.daraz.com.bd/p/\w+.jpg,所以我们可以使用正则表达式来提取这些 URL。问题是你可能会得到更多你想要的图像,除非你制作更多的过滤器来得到你想要的东西。这是一种方法,剩下的就靠你了:

import requests
import json
import re
import urllib 
import os

url = 'https://www.daraz.com.bd/products/awei-y336-portable-wireless-bluetooth-mini-speaker-charging-cable-i128428097-s1048302559.html?search=1'
resp1 = requests.get(url)
text = resp1.text
images = list(set(re.findall(r"https://static-01.daraz.com.bd/p/\w+.jpg", text)))
for i, img in enumerate(images):
    urllib.request.urlretrieve(img, filename=f'img{i}.jpg')

【讨论】:

  • 不工作/收到此错误 FileNotFoundError: [Errno 2] No such file or directory: 'images/img0.jpg'
  • 创建一个名为images的文件夹
  • 我修改了,试试看。如果你有错误,请告诉什么错误。
  • Juan Javier Santos Ochoa 谢谢,但问题是它正在下载图片。我想要的是打印图片网址而不是下载
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-14
相关资源
最近更新 更多