求解下面错误解决办法:
Traceback (most recent call last):
File "E:\python\untitled\test_photo.py", line 18, in
img_url = img['stc']
File "E:\python\python\lib\site-packages\bs4\element.py", line 1406, in getitem
return self.attrs[key]
KeyError: 'stc'
r = requests.get(url=url,headers=headers)
print(r) #返回200表示成功发送请求
r.encoding = "utf-8"
page_text = r.text
soup = BeautifulSoup(r.content,'lxml')
#print(soup)
all_img = soup.select("div.list ul a img")
#print(all_img.text)
for img in all_img:
img_url = img['stc']
print(img_url)
root = r'E:\python\untitled\ji' #图片存储根目录
path = root + img_url.split('/')[0] #重命名
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
print(url)
r = requests.get(img_url)
print(r)
with open(path,'wb')as f:
f.write(r.content)
f.close()
print("文件保存成功!")
else:
print("文件已存在!")
else:
print("爬取失败!!!")
except:
print("error")