AttributeError:“ NoneType”对象的BeautifulSoup4没有属性“ group”

时间:2019-09-25 07:51:19

标签: python beautifulsoup

您好社区,我有一个问题,我不知道该如何解决。我的问题是我编写了一个脚本来检索具有BeautifuleSoup4的图像的网页,但出现错误(AttributeError:“ NoneType”对象没有属性“ group”)

import re
import requests
from bs4 import BeautifulSoup

site = 'https://www.fotocommunity.de/natur/wolken/3144?sort=new'

response = requests.get(site)

soup = BeautifulSoup(response.text, 'html.parser')
img_tags = soup.find_all('img', {"src": True})

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

for url in urls:
    filename = re.search(r'([\w_-]+[.](jpg|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)

1 个答案:

答案 0 :(得分:1)

您的正则表达式是错误的。如果您不熟悉正则表达式,请使用Python内部的urllib进行繁重的工作,而不是编写正则表达式。

使用这样的内容(未经测试):

import re
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlsplit  # import this additional library
from os.path import basename  # import this additional library

site = 'https://www.fotocommunity.de/natur/wolken/3144?sort=new'

response = requests.get(site)

soup = BeautifulSoup(response.text, 'html.parser')
images_div = soup.find(id=re.compile(r"fcx-gallery-\w+"))  # focus on the div containing the images
if img_tags:  # test if img_tags has any data
    img_tags = images_div.find_all('img', {"data-src": True})  # get all the images in that div

    urls = [img["data-src"] for img in img_tags]  # grab sources from data-source

    for url in urls:
        filename = basename(urlsplit(url).path)  # use this instead of a regex
        with open(filename, 'wb') as f:  # filename is now a string

            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)