【问题标题】:How do I scrape specific images (excluding thumbnails,icons,etc.) from forums using Beautiful soup如何使用 Beautiful soup 从论坛中抓取特定图像(不包括缩略图、图标等)
【发布时间】:2015-12-25 09:36:28
【问题描述】:

例如,我想获取论坛http://www.xossip.com/showthread.php?t=1384077 的所有图片的链接。

当我检查图片(来自论坛帖子的大图)时,他们在常见的<img src="http://pzy.be/i/5/17889.jpg" border="0" alt=""> 上有类似的东西。

程序应该是什么来列出所需图像的所有 URL。如果可能的话,甚至下载它们。

我尝试了一些代码但卡住了。

import requests
from bs4 import BeautifulSoup

def spider(max_pages):
    page = 1
    while page <= max_pages:
        url = 'http://www.xossip.com/showthread.php?t=1384077&page=' + str(page)
        sourcecode= requests.get(url)
        plaintext = sourcecode.text
        soup = BeautifulSoup(plaintext)
        for link in soup.findAll('img src'):
            print (link)
        page += 1
spider(1)

编辑 我想要论坛中的图片,但我想避免所有那些小缩略图、徽标、图标等。我观察到我需要的所有图像都有这种格式&lt;img src="http://pzy.be/i/5/17889.jpg" border="0" alt=""&gt; 所以我需要上述格式的图像的所有链接,所以我需要程序遍历论坛的所有页面,使用 src、border=0、alt 优化图像,最后打印所有图像 url,如 pzy.be/ i/5/452334.jpg

【问题讨论】:

  • 请在此处发布代码,并查看stackoverflow.com/help/mcve
  • 对不起,我是新来的,我无法理解如何在此处粘贴代码
  • @sobolevn 完成添加

标签: python web-scraping beautifulsoup web-crawler


【解决方案1】:

尝试使用tag.get('src') 而不是soup.findAll('img src')

import requests
from bs4 import BeautifulSoup

def spider(max_pages):
    page = 1
    while page <= max_pages:
        url = 'http://www.xossip.com/showthread.php?t=1384077&page=' + str(page)
        sourcecode= requests.get(url)
        plaintext = sourcecode.text
        soup = BeautifulSoup(plaintext)

        for tag in soup.findAll('img'): 
            print(tag.get('src'))   # use `tag.get('src')` in this case

        page += 1
spider(1)

详情请查看the document


如果您需要下载它们,您也可以使用requests 下载图像的内容,并将其写入文件。这是一个演示:

import requests
from bs4 import BeautifulSoup

def spider(max_pages):
    page = 1
    while page <= max_pages:
        url = 'http://www.xossip.com/showthread.php?t=1384077&page=' + str(page)
        sourcecode= requests.get(url)
        plaintext = sourcecode.text
        soup = BeautifulSoup(plaintext)

        for tag in soup.findAll('img'):
            link = tag.get('src')  # get the link

            # Check if the tag is in expect format
            del tag['src']
            if tag.attrs != {';': '', 'alt': '', 'border': '0'}:
                continue

            filename = link.strip('/').rsplit('/', 1)[-1]  # to get the correct file name

            image = requests.get(link).content  # use requests to get the content of the images
            with open(filename, 'wb') as f:
                f.write(image)  # write the image into a file

        page += 1
spider(1)

【讨论】:

  • Traceback(最近一次调用最后):文件“/Users/Taarush/PycharmProjects/First/first.py”,第 13 行,在 spider(1) 文件“/Users/Taarush/ PycharmProjects/First/first.py",第 6 行,在 spider sourcecode= requests.get(url) NameError: name 'requests' is not defined
  • @TaarushV 你忘了import requests吧?
  • 谢谢它确实有效。但我只想要大的图像而不是图标和缩略图,我只想要那些有 pzy.be/i/6/17885.jpg"border="0" alt=""> 的图像。你知道边界“0”。我如何具体得到它们,再次感谢
  • @TaarushV:那么,例如,获取所有具有border="0" 的图像?
  • 我的意思是我想要不包括图标等的图像有 border="0" 和 alt="" 和 pzy.be host common 。我应该在代码中过滤它们
猜你喜欢
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2022-01-03
  • 2018-08-05
  • 2016-02-14
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多