python KeyError: 'src'

albert陈 2017-06-12 08:08:39
这是我的代码

import requests
from bs4 import BeautifulSoup

html_url="http://www.xghylt.com/thread-7085136-1-1.html"
headers={'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"}
response=requests.get(html_url,headers=headers)
print(response)
#print(response.text)
all_img=BeautifulSoup(response.text,'lxml').find('div',class_="t_fsz").find_all('img')
lenth = len(all_img) #计算集合的个数
for i in range(lenth):
img_url=all_img[i].attrs['src']

print("kaishi"+img_url)
print(img_url)

这是我的控制台的错误提示
Traceback (most recent call last):
File "I:/Python/Py/huaiyinluntan/meitu.py", line 12, in <module>
img_url=all_img[i].attrs['src']
KeyError: 'src'


...全文
902 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

3,424

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧