Python爬虫实战(五) :下载百度贴吧帖子里的所有图片
2017-09-05 12:31
981 查看
准备工作:
目标网址:https://tieba.baidu.com/p/5113603072
目的: 下载该页面上的所有楼层里的照片
第一步:分析网页源码
火狐浏览器 ---> 在该页面上右击 “查看页面源代码”,会打开一个新的标签页。
第二步:查找图片源地址
在新标签页上ctrl + F,输入jpg,找到第一个图片的源地址
<img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=29a773eb871001e94e3c1407880f7b06/50cf3bc79f3df8dc5b6bb593c711728b47102859.jpg"
BTW,怎么知道这个链接是不是第一张图,在查找jpg的时候,直接复制jpg网址在浏览器上输入回车对比即可,如果是超链接,直接点击即可。
查找后分析,所有楼层里面的图片都是这个格式<img class="BDE_Image" src=.......
第三步:使用强大BeautifulSoup,过滤标签img 和 class="BDE_Image",代码:
soup = BeautifulSoup(html, 'html.parser')
img_info = soup.find_all('img', class_='BDE_Image')
第四步:找出所有照片的原始链接,代码
for img in img_info:
img.get("src")
第五步:下载
使用urllib.request.urlretrieve函数
整合后写成完整代码:
我们看到该帖子远远不止一页,我们也想下载其他页面上的图片怎么办?
点击第二页,网址变为
https://tieba.baidu.com/p/5113603072?pn=2
将pn=2改成pn=1试试,回车,又发现与之前的网站https://tieba.baidu.com/p/5113603072指向的是同一个页面,因此可以循环来访问了。
代码:
总结:BeautifulSoup功能真是太强大了。
目标网址:https://tieba.baidu.com/p/5113603072
目的: 下载该页面上的所有楼层里的照片
第一步:分析网页源码
火狐浏览器 ---> 在该页面上右击 “查看页面源代码”,会打开一个新的标签页。
第二步:查找图片源地址
在新标签页上ctrl + F,输入jpg,找到第一个图片的源地址
<img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=29a773eb871001e94e3c1407880f7b06/50cf3bc79f3df8dc5b6bb593c711728b47102859.jpg"
BTW,怎么知道这个链接是不是第一张图,在查找jpg的时候,直接复制jpg网址在浏览器上输入回车对比即可,如果是超链接,直接点击即可。
查找后分析,所有楼层里面的图片都是这个格式<img class="BDE_Image" src=.......
第三步:使用强大BeautifulSoup,过滤标签img 和 class="BDE_Image",代码:
soup = BeautifulSoup(html, 'html.parser')
img_info = soup.find_all('img', class_='BDE_Image')
第四步:找出所有照片的原始链接,代码
for img in img_info:
img.get("src")
第五步:下载
使用urllib.request.urlretrieve函数
整合后写成完整代码:
#coding=utf-8 import requests import urllib.request from bs4 import BeautifulSoup def getHtml(url): page = requests.get(url) html =page.text return html def getImg(html): soup = BeautifulSoup(html, 'html.parser') img_info = soup.find_all('img', class_='BDE_Image') for index,img in enumerate(img_info,1): print ("正在下载第{}张图片".format(index)) urllib.request.urlretrieve(img.get("src"),'%s.jpg' % index) if __name__=='__main__': url = "https://tieba.baidu.com/p/5113603072" html = getHtml(url) getImg(html) print ("OK!All DownLoad!")
我们看到该帖子远远不止一页,我们也想下载其他页面上的图片怎么办?
点击第二页,网址变为
https://tieba.baidu.com/p/5113603072?pn=2
将pn=2改成pn=1试试,回车,又发现与之前的网站https://tieba.baidu.com/p/5113603072指向的是同一个页面,因此可以循环来访问了。
代码:
#coding=utf-8 import re import requests import urllib.request from bs4 import BeautifulSoup def getHtml(url): page = requests.get(url) html =page.text return html def getImg(html): soup = BeautifulSoup(html, 'html.parser') img_info = soup.find_all('img', class_='BDE_Image') global index for index,img in enumerate(img_info,index+1): print ("正在下载第{}张图片".format(index)) urllib.request.urlretrieve(img.get("src"),'%s.jpg' % index) def getMaxPage(url): html = getHtml(url) reg = re.compile(r'max-page="(\d+)"') page = re.findall(reg,html) page = int(page[0]) return page if __name__=='__main__': url = "https://tieba.baidu.com/p/5113603072" page = getMaxPage(url) index = 0 for i in range(1,page): url = "%s%s" % ("https://tieba.baidu.com/p/5113603072?pn=",str(i)) html = getHtml(url) getImg(html) print ("OK!All DownLoad!")
总结:BeautifulSoup功能真是太强大了。
相关文章推荐
- [置顶] 【python 爬虫】百度贴吧帖子所有楼层图片爬虫
- python3 多页爬虫爬取百度贴吧帖子所有图片
- Python实现爬取百度贴吧帖子所有楼层图片的爬虫示例
- Python爬虫实战(1)——百度贴吧抓取帖子并保存内容和图片
- Python爬虫实战二:下载百度贴吧帖子内的壁纸
- Python爬虫实战(2):百度贴吧帖子
- Python爬虫实战一之爬取百度贴吧中图片
- python爬虫:下载百度贴吧图片学习笔记
- Python爬虫实战二之爬取百度贴吧帖子
- Python实现简单爬虫功能--批量下载百度贴吧里的图片
- 我的第一个python爬虫程序(从百度贴吧自动下载图片)
- python爬虫实战二之爬取百度贴吧帖子
- Python爬虫实战之爬取百度贴吧帖子
- 芝麻HTTP:Python爬虫实战之爬取百度贴吧帖子
- Python下载百度贴吧帖子里面的图片
- 使用简易Python爬虫下载百度贴吧图片
- Python爬虫实战二之爬取百度贴吧帖子
- 【Python3.6爬虫学习记录】(四)爬取百度贴吧某帖子内容及图片
- Python爬虫实战:百度贴吧帖子
- Python爬虫实战二之爬取百度贴吧帖子