【问题标题】:BeautifulSoup, Scraping, get image size without being in the tags?BeautifulSoup,Scraping,获取图像大小而不在标签中?
【发布时间】:2017-12-17 03:54:41
【问题描述】:

目标:http://voorraadmodule.vwe-advertentiemanager.nl/s9376368b43e8fd6a8025bfa284d8e732/e7c2/stock/vehicles/100/ 我学习python 8 天了,真的很喜欢。目标页面是我的老雇主,为了测试我的技能,我想编写一个 python 程序,每天检查他的库存并给我变化的结果(已售出、降价等)

对我来说,可以获得我想要的所有参数,除了正在出售但尚未从页面中删除的汽车的标记/触发器。

当您访问目标页面时,您会看到一些图像带有带有“verkocht”的功能区。我搜索了所有的 HTML,代码中没有触发它是否出售,CMS 只用功能区更改图片。我确实注意到当这种情况发生时缩略图会改变大小,所以我希望这是我的触发器。

部分代码:

from bs4 import BeautifulSoup
import requests

url = "http://voorraadmodule.vwe-advertentiemanager.nl/s9376368b43e8fd6a8025bfa284d8e732/e7c2/stock/vehicles/100/"
img_pre_url = "http://voorraadmodule.vwe-advertentiemanager.nl/s4c74bf131813e9d7d3232b46224830a2"
getpage = requests.get(url)
soup = BeautifulSoup(getpage.text, "html.parser")

for listingparse in soup.find_all("div", class_="row clearfix "):

    ftch_id = listingparse.get("id")[8:]

    ftch_imgurl = listingparse.find("div", class_="columnPhoto").img["src"]

    print("List id: "+ftch_id + "Image url: "+img_pre_url+ftch_imgurl)

得到了这部分用于在原始版本中演示这一点,我将它与更多参数一起写入 csv。

最终目标是获得变量 'sold_marker: V' 表示已售出或 'sold_marker: X' 表示当前列表

所以我作为菜鸟认为我有 2 个选项。 1.下载图片并用numpy测量大小 2. 使用一些图像处理库,如果存在色带,则使用难看的绿色进行测量。

你们会如何处理这个问题?我希望不必每天下载图像来衡量这一点,但我想别无选择。

【问题讨论】:

  • 似乎图像在其 url 中有大小 - 即。 .../320x213/... 所以你甚至不必下载它们。
  • 谢谢furas,没注意到!这对于我的解决方案就足够了,我会留下这个问题,因为我很好奇如何解决这个学习难题。
  • 按大小对图像进行分组并在文件夹名称中使用此大小非常流行。

标签: python numpy beautifulsoup screen-scraping


【解决方案1】:

图像大小在其 url 中,因此您可以使用 "/" 拆分 url 并从列表中获取大小。

from bs4 import BeautifulSoup
import requests

url = "http://voorraadmodule.vwe-advertentiemanager.nl/s9376368b43e8fd6a8025bfa284d8e732/e7c2/stock/vehicles/100/"
img_pre_url = "http://voorraadmodule.vwe-advertentiemanager.nl/s4c74bf131813e9d7d3232b46224830a2"
getpage = requests.get(url)
soup = BeautifulSoup(getpage.text, "html.parser")

for listingparse in soup.find_all("div", class_="row clearfix "):

    ftch_id = listingparse.get("id")[8:]
    ftch_imgurl = listingparse.find("div", class_="columnPhoto").img["src"]

    url_parts = ftch_imgurl.split('/')

    if url_parts[-2] == "260x195":
        verkocht = "verkocht"
    else:
        verkocht = ""

    print("List id:", ftch_id)
    print("Image url:", img_pre_url+ftch_imgurl)
    print("image size:", url_parts[-2], verkocht)
    print('---')

结果:

List id: 15668794
Image url: http://voorraadmodule.vwe-advertentiemanager.nl/s4c74bf131813e9d7d3232b46224830a2/vehicle-images/15668794/1/1513180329/320x213/citroen-xsara-picasso-1-6i-attraction-zeer-ruime-gezinsauto
image size: 320x213 
---
List id: 15529833
Image url: http://voorraadmodule.vwe-advertentiemanager.nl/s4c74bf131813e9d7d3232b46224830a2/vehicle-images/15529833/1/1512131899/260x195/dacia-logan-mcv-1-6-laureate-zeer-ruime-buitenkans
image size: 260x195 verkocht
---
List id: 15427090
Image url: http://voorraadmodule.vwe-advertentiemanager.nl/s4c74bf131813e9d7d3232b46224830a2/vehicle-images/15427090/1/1510153600/320x213/fiat-punto-evo-1-3-m-jet-dynamic
image size: 320x213 
---
List id: 15287283
Image url: http://voorraadmodule.vwe-advertentiemanager.nl/s4c74bf131813e9d7d3232b46224830a2/vehicle-images/15287283/1/1508421733/320x213/hyundai-matrix-1-6i-active-ek-2008-automaat-parkeersensoor-achter
image size: 320x213 
---
List id: 15218532
Image url: http://voorraadmodule.vwe-advertentiemanager.nl/s4c74bf131813e9d7d3232b46224830a2/vehicle-images/15218532/1/1513263561/260x195/land-rover-range-rover-sport-3-6-tdv8-hse-vol-met-opties
image size: 260x195 verkocht
---
List id: 13888171
Image url: http://voorraadmodule.vwe-advertentiemanager.nl/s4c74bf131813e9d7d3232b46224830a2/vehicle-images/13888171/1/1491479399/320x213/maserati-quattroporte-4-7-s
image size: 320x213 
---

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多