【发布时间】:2019-09-25 07:51:19
【问题描述】:
你好社区我有一个问题,我不知道如何解决我的问题是我编写了一个脚本来抓取带有 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)
【问题讨论】:
-
这意味着您对
filename的正则表达式搜索没有返回任何结果。首先使用if测试其真实性。 -
但是我的问题是我没有得到任何照片并且没有错误:/
-
因为你的正则表达式是错误的。我猜你只想要文件名?
-
是的,我只想拥有文件名。抱歉,这是我的第一个爬虫 :)
-
酷。在下面检查我的答案。
标签: python beautifulsoup