【问题标题】:Unable to download all images using Beautiful Soup无法使用 Beautiful Soup 下载所有图像
【发布时间】:2018-10-29 18:07:43
【问题描述】:

我对数据抓取不是很熟悉,我无法使用漂亮的汤下载图像。

我需要从网站下载所有图片。我正在使用以下代码:

import re
import requests
from bs4 import BeautifulSoup

site = 'http://someurl.org/'

response = requests.get(site)

soup = BeautifulSoup(response.text, 'html.parser')

# img_tags = soup.findAll('img')
img_tags = soup.findAll('img',{"src":True})

print('img_tags: ')
print(img_tags)

urls = [img['src'] for img in img_tags]

print('urls: ')
print(urls)

for url in urls:
    filename = re.search(r'/([\w_-]+[.](jpg|gif|png))$', url)
    with open(filename.group(1), 'wb') as f:
        if 'http' not in url:
            # sometimes an image source can be relative 
            # if it is provide the base url which also happens 
            # to be the site variable atm. 
            url = '{}{}'.format(site, url)
        response = requests.get(url)
        f.write(response.content)

但是,这会忽略页面上所有具有与此类似的 html 的图像:

<img data-bind="attr: { src: thumbURL() }" src="/assets/images/submissions/abfc-2345345234.thumb.png">

我认为这是因为 data 属性也包含字符串“src”,但我似乎无法弄清楚。

【问题讨论】:

  • 如果这是实际的 HTML 代码,则其中包含不平衡的引号。因此,您需要检索所有带有数据绑定值的 img 标签,并且必须提取正确的值。
  • 我刚刚编辑过,我不小心删除了引用但这是我在检查页面时看到的。
  • 能走多远?是打印img_tags,还是urls
  • 它会打印 img_tags 和 url,但完全忽略了我关心的图像,只打印页面中的社交媒体图标图像。

标签: python beautifulsoup


【解决方案1】:

你需要使用 selenium 或者一些可以运行 javascript 的。这是代码加载图像,直到找到它

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

site = 'http://phylopic.org/'
dr = webdriver.Chrome()

dr.get(site)
try:
    element = WebDriverWait(dr, 20, 0.5).until(
        EC.visibility_of_element_located((By.CLASS_NAME, "span1"))
    )
except:
    print("Wait a bit more")
    time.sleep(5)

text = dr.page_source
soup = BeautifulSoup(text,"lxml")
imgs = soup.find_all('img')
print(imgs)

dr.close()

第二个问题是如何将相对路径转换为绝对路径。 HTML 上有几种类型的relative path

当网址为http://someurl.org/somefd/somefd2

  • &lt;img src="picture.jpg"&gt; http://someurl.org/somefd/somefd2/picture.jpg
  • &lt;img src="images/picture.jpg"&gt; http://someurl.org/somefd/somefd2/images/picture.jpg
  • &lt;img src="/images/picture.jpg"&gt; http://someurl.org/images/picture.jpg
  • &lt;img src="../picture.jpg"&gt; http://someurl.org/somefd/picture.jpg

这是我将 rp 转换为 ap 的代码。

import re

site = 'https://en.wikipedia.org/wiki/IMAGE'


def r2a(path,site=site):
    rp = re.findall(r"(/?\W{2}\/)+?",path)

    if path.find("http") == 0: 
        #full http url
        return path

    elif path.find("//") == 0: 
        #http url lack of http:
        return "http:" + path

    elif path.find("//") < 0 and path.find("/") == 0: 
        # located in the folder at the root of the current web
        site_root = re.findall("http.{3,4}[^/]+",site)
        return site_root[0] + path

    elif rp: 
        # located in the folder one level up from the current folder
        sitep = len(re.findall(r"([^/]+)+",site)) - 2 - len(rp)
        # raise error when sitep-len(rp)
        new_path = re.findall("(http.{4}[^/]+)(/[^/]+){%d}"%(sitep),site)
        return "{}/{}".format("".join(new_path[0]),path.replace( "".join(rp) , ""))

    else:
        #  located in the folder one level up from the current folder
        #  located in the same folder as the current page
        return "{}/{}".format(site,path)


assert "https://en.wikipedia.org/wiki/IMAGE/a.jpg" == r2a("a.jpg")
assert "https://en.wikipedia.org/wiki/IMAGE/unknow/a.jpg" == r2a("unknow/a.jpg")
assert "https://en.wikipedia.org/unknow/a.jpg" == r2a("/unknow/a.jpg")
assert "https://en.wikipedia.org/wiki/a.jpg" == r2a("../a.jpg")
assert "https://en.wikipedia.org/a.jpg" == r2a("../../a.jpg")
assert "https://en.wikipedia.org/wiki/IMAGE/a.jpg" == r2a("https://en.wikipedia.org/wiki/IMAGE/a.jpg")
assert "http://en.wikipedia.org/" == r2a("//en.wikipedia.org/")

【讨论】:

  • 在我看来问题在于 img_tags = soup.findAll('img',{"src":True}) 因为我无法获取图像标签,也无法获取其相对或绝对路径。
  • 你能提供一个示例网址吗?
  • 我正在尝试从这里获取图标phylopic.org 图片有 html:
  • 获取不到的原因是这个img需要javascript来加载数据。 @user2300867
  • 我正在寻找我回答的问题,这可以帮助我写出更好的答案
猜你喜欢
  • 1970-01-01
  • 2021-08-13
  • 2016-02-14
  • 2018-10-05
  • 2016-07-06
  • 2016-01-09
  • 2020-05-13
  • 1970-01-01
相关资源
最近更新 更多