AttributeError: у объекта 'NoneType' нет атрибута 'group' с BeautifulSoup4 - PullRequest
0 голосов
/ 25 сентября 2019

Hello Community У меня есть проблема, и я не знаю, как ее решить. Моя проблема - я пишу скрипт для сканирования веб-страниц для изображений с 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 Ответ

1 голос
/ 25 сентября 2019

Ваше регулярное выражение неверно.Используйте внутренний 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...