Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';非类型';对象没有属性';集团';与美丽的4_Python_Beautifulsoup - Fatal编程技术网

Python 属性错误:';非类型';对象没有属性';集团';与美丽的4

Python 属性错误:';非类型';对象没有属性';集团';与美丽的4,python,beautifulsoup,Python,Beautifulsoup,你好社区我有一个问题,我不知道如何解决我的问题是我写了一个脚本,用BeautifulSoup4抓取网页上的图像,但我得到了错误(AttributeError:'NoneType'对象没有属性'group') 你的正则表达式错了。如果您不熟悉正则表达式,可以使用Python的内部urllib来执行重量级提升,而不是编写正则表达式 使用类似的方法(未经测试): 重新导入 导入请求 从bs4导入BeautifulSoup 从urllib.parse导入urlspilt#导入此附加库 从os.path

你好社区我有一个问题,我不知道如何解决我的问题是我写了一个脚本,用BeautifulSoup4抓取网页上的图像,但我得到了错误(AttributeError:'NoneType'对象没有属性'group')


你的正则表达式错了。如果您不熟悉正则表达式,可以使用Python的内部
urllib
来执行重量级提升,而不是编写正则表达式

使用类似的方法(未经测试):

重新导入
导入请求
从bs4导入BeautifulSoup
从urllib.parse导入urlspilt#导入此附加库
从os.path import basename#导入此附加库
场地https://www.fotocommunity.de/natur/wolken/3144?sort=new'
response=requests.get(站点)
soup=BeautifulSoup(response.text'html.parser')
images_div=soup.find(id=re.compile(r“fcx gallery-\w+”)#关注包含图像的div
如果img_标签:#测试img_标签是否有任何数据
img_tags=images_div.find_all('img',{“data src”:True})#获取该div中的所有图像
URL=[img[“data src”]用于img#u标记中的img]#从数据源抓取源
对于url中的url:
filename=basename(urlspit(url.path)#使用此选项而不是正则表达式
将open(filename,'wb')作为f:#filename现在是一个字符串
如果url中没有“http”:
#有时图像源可能是相对的
#如果是,请提供也会发生的基本url
#作为站点变量atm。
url='{}{}'。格式(站点,url)
response=requests.get(url)
f、 写(response.content)

这意味着您对
文件名的正则表达式搜索没有返回任何结果。首先用
if
测试它的真实性。但是我有一个问题,我没有得到任何照片,也没有错误:,因为你的正则表达式是错误的。我猜你只想要文件名?是的,我只想要文件名。对不起,这是我的第一个爬虫:)酷。检查我下面的答案。很酷,它可以工作,但我没有得到每一张照片只有10张,但我需要一点“更多”:你检查过你有多少
url
?我在第13行测试了你的代码,共有6个url。你在哪里找到这么多url?我只能找到url标记和爬网的url。现在我遇到了这个错误(KeyError:'src')我知道我必须首先允许它,但在哪里?我的错。我错误地删除了
{“src”:True}
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)