python爬虫——获取图片2
来源:互联网 发布:王陆807和语料库 知乎 编辑:程序博客网 时间:2024/06/05 06:48
不同的网址,主要是headers 需要修改
BeautifulSoup查找的标签,需要进入开发者工具界面,定位出图片所在的标签。
代码在pthon3.5 上能直接用
import requests ##导入requestsfrom bs4 import BeautifulSoup ##导入bs4中的BeautifulSoupimport osheaders = {'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}all_url = 'https://www.pexels.com/search/leaf/'start_html = requests.get(all_url, headers=headers) ##使用requests中的get方法来获取all_url(就是:https://www.pexels.com/search/leaf/这个地址)的内容 headers为上面设置的请求头、请务必参考requests官方文档解释#print(start_html.text) ##打印出start_html (请注意,concent是二进制的数据,一般用于下载图片、视频、音频、等多媒体内容是才使用concent, 对于打印网页内容请使用text)Soup = BeautifulSoup(start_html.text, 'lxml') ##使用BeautifulSoup来解析我们获取到的网页(‘lxml’是指定的解析器 具体请参考官方文档哦)##使用BeautifulSoup解析网页过后就可以用找标签呐!(find_all是查找指定网页内的所有标签的意思,find_all返回的是一个列表。)x=1all_a = Soup.find('div', class_='photos').find_all('a') ##意思是先查找 class为 all 的div标签,然后查找所有的<a>标签。for a in all_a: img = a.find_all('img')[0] #print(img) title = img['data-pin-media'] #取出img标签属性 src = img['src'] #取出img标签属性src print(title,src) html = requests.get(src,headers=headers) html_soup = BeautifulSoup(html.text, 'lxml') #urllib.request.urlretrieve(image['src'],"E:\\WORK\\py\\TensorFlow\\爬取树叶图片\\BS-Soup-gaijin\\%s.jpg"%(x)) name = "E:\\WORK\\py\\TensorFlow\\爬取树叶图片\\BS-Soup-gaijin\\" f = open(name+'%s.jpg'%(x), 'ab')##写入多媒体文件必须要 b 这个参数!!必须要!! f.write(html.content) ##多媒体文件要是用conctent哦! f.close() x = x+1
阅读全文
0 0
- python爬虫——获取图片2
- python 爬虫获取网页图片
- Python 爬虫:获取网页图片
- Python Requests爬虫——获取一个收藏夹下所有答案的图片
- Python爬虫(5)——获取京东手机图片
- Python 爬虫获取百度贴吧图片
- python爬虫 分页获取图片并下载
- python简单爬虫(获取图片)
- python 爬虫 获取网页中的图片
- Python爬虫入门——爬取贴吧图片
- python爬虫——BeautifulSoup 抓取图片
- python爬虫——获取新闻标题
- python爬虫——获取正文内容
- Python爬虫_简单获取百度贴吧图片
- Python爬虫获取图片并下载保存至本地
- python爬虫(9)获取动态搞笑图片
- Python+爬虫技术获取斗鱼直播图片(整理)
- Python爬虫个人记录(三)获取图片
- [Winform]WebKit.Net使用
- ArcGIS中的一些影像格式:.ige .img .rde .rrd
- 读取properties文件的内容值
- grunt压缩、合并css、js文件的配置信息
- 观察者模式和事件通知备忘
- python爬虫——获取图片2
- [NOIP模拟赛]单向道路
- 褚时健84岁再成亿万富翁
- JavaScript基础
- MySQL的InnoDB索引原理详解
- 界面编码报错:Page-encoding specified in XML prolog (UTF-8) is different from that specified in
- python学习十(多继承,多态,异常)
- Mybatis--更新
- Balanced Numbers 数位DP+3进制压缩