|
10鱼币
本帖最后由 wmy1212 于 2021-10-17 20:31 编辑
代码奉上,报错在20行:
- import requests # 发请求到服务器
- from bs4 import BeautifulSoup
- resp = requests.get('https://www.umei.cc/katongdongman/') #从服务器拿到源码
- resp.encoding = 'utf-8'
- # 解析html
- main_page = BeautifulSoup(resp.text,'html.parser')
- # 从页面中找到某些东西
- # find() find_all()
- alist = main_page.find('div',attrs = {'class':'TypeList'}).find_all('a',attrs = {'class':'TypeBigPics'})
- # div是标签,后面是属性,是一一对应的
- n = 1
- for i in alist:
- # print(i.get('href'))
- #发送请求到子页面,进入子页面
- href = 'https://www.umei.cc/katongdongman/' + i.get('href')
- resp1 = requests.get(href)
- resp1.encoding = 'utf-8'
- child_page = BeautifulSoup(resp1.text,'html.parser')
- print(type(child_page))
- src = child_page.find('div',attrs = {'class':'ImageBody'}).find('img').get('src') # 问题出在这一行!!!!
-
- # 发送请求到服务器,把图片保存在本地
- # requests.get(src).content 向外拿出图片的数据,不是文本信息
-
- f = open('图_%d.jpg' % n,mode = 'wb')# wb表示写入的内容是非文本文件
- f.write(requests.get(src).content)
- print('恭喜你,下载了%d张图片!' % n)
- n += 1
-
- f.close()
复制代码
发生了报错:
- AttributeError Traceback (most recent call last)
- <ipython-input-6-16053cc4abc5> in <module>()
- 7 child_page = BeautifulSoup(resp1.text,'html.parser')
- 8 print(type(child_page))
- ----> 9 src = child_page.find('div',attrs = {'class':'ImageBody'}).find('img').get('src')
- 10 # 发送请求到服务器,把图片保存在本地
- 11 # requests.get(src).content 向外拿出图片的数据,不是文本信息
- AttributeError: 'NoneType' object has no attribute 'find'
复制代码
两个问题:
1.为什么说我的chila_page是NoneType?明明我打印了是<class 'bs4.BeautifulSoup'>
2.这个程序如果de完了bug,我用jupyter notebook跑出来之后,文件是保存在jupyter所在的文件夹里吗?
|
|