python抓取网站88titienmae88中的“图片区”所有图片
2018-01-12 11:09
459 查看
#-*-coding:utf-8-*- from urllib.request import urlopen, urlretrieve from bs4 import BeautifulSoup import re import os ''' 抓取网站http://jyghf.com/中的“图片区”的的所有图片 关于这个网站,大家不要太有纠结的情绪,作为一个泱泱大国的男士,第一个爬虫,必须要有作为,作为啊!!! ''' ''' 第一步,进入到图片类中:根据http://jyghf.com/的html分析: 在id='top_box'的div中,第一个class='menu'的div包含着所有的“图片区”分类。 这些分类的url都是以“/p”开头,如:/p01/index.html,全路径: http://jyghf.com/p01/index.html ''' ''' 第二步,进入到图片文件夹中:根据http://jyghf.com/p01/index.html的html分析 在class="typelist"的div中,图片路径都在“<li>”标签中,这些图片路径都是以“/htm/”开头 如:“/htm/2017/12/13/p01/393067.html”,全路径:“http://jyghf.com/p01/index.html/htm/2017/12/13/p01/393067.html” ''' ''' 第三步,获取图片的下载路径:根据html分析,图片路径,都在id="view1"的div中的<img>标签的“src”属性下。 ''' # 第一步,进入到图片类中 def getPicTypeLink(): html=urlopen("http://jyghf.com/") bshtml=BeautifulSoup(html,"html.parser") picTypes=bshtml.find("div",{"id":"top_box"}).find("div",{"class":"menu"})\ .findAll("a",href=re.compile("^(/p)")) # html.close() return picTypes # 第二步,进入到图片文件夹中 def getPicFileLink(typeLink): html=urlopen("http://jyghf.com/{0}".format(typeLink)) bshtml=BeautifulSoup(html,"html.parser") picfiles=bshtml.find("div",{"class":"typelist"}).findAll("a",href=re.compile("^(/htm/)")) # html.close() return picfiles # 第三步,获取图片的下载路径 def getPicSrcLink(picfilelink): html=urlopen("http://jyghf.com/{0}".format(picfilelink)) bshtml=BeautifulSoup(html,"html.parser") srcLinks=bshtml.find("div",{"id":"view1"}).findAll("img",src=re.compile("^(http://)")) # html.close() return srcLinks #辅助:根据图片类型+第几页+图片文件夹,创建文件目录 def getDownloadPath(typename,pageid,filename,downLoadDirectory="E:\downloaded"): path="{0}/{1}/第{2}页/{3}/".format(downLoadDirectory,typename,pageid,filename) directory = os.path.dirname(path) if not os.path.exists(directory): os.makedirs(directory) return path #下载图片:根据图片路径的列表(list),下载每个路径的图片 def downloadPic(picSrcLinks,typename,pageid,filename): pid = 1 for picsrc in picSrcLinks: downloadurl = picsrc.attrs["src"] print("第{0}张图片".format(pid)) urlretrieve(downloadurl, "{0}/{1}.jpg".format(getDownloadPath(typename,pageid,filename), pid)) pid += 1 #主体程序 def download(link): typeLink=link.attrs["href"] typename=link.get_text() print(typename) picFileLinks=getPicFileLink(typeLink) pageid=1 fileid=1 for picfile in picFileLinks: if len(picFileLinks)>0: picFileLink=picfile.attrs["href"] filename=picfile.get_text() print("第{0}页:{1}".format(pageid,filename)) fileid+=1 picSrcLinks=getPicSrcLink(picFileLink) downloadPic(picSrcLinks,typename,pageid,filename) #获取“下一页”的链接 html = urlopen("http://jyghf.com/{0}".format(typeLink)) bshtml = BeautifulSoup(html, "html.parser") nextpage = bshtml.find("div", {"id": "page"}).find("a", title="下一页") while len(nextpage)>0: pageid+=1 download(nextpage) #######开始######## picTypeLinks=getPicTypeLink() for pictypelink in picTypeLinks: download(pictypelink)
相关文章推荐
- python抓取网站88titienmae88中的“图片区”的第一页的所有图片
- 【Python】爬虫入门--抓取网站图片
- 最基础的python抓取网站图片例子
- <四>、python爬虫抓取购物网站商品信息--图片价格名称
- Python2.7 自动抓取某贴吧所有帖子的图片
- python学习 二 03 爬一个图片网站-获得所有分页网址
- 用python下载xxxx网站封面作品的所有图片
- Python抓取妹子图网站全部图片
- python抓取网站的图片并下载到本地的方法
- python学习——爬下网站的所有图片并保存在本地
- Python入门-编写抓取网站图片的爬虫-正则表达式
- 使用python来批量抓取网站图片
- 基础的python抓取网站图片的例子
- python 实现网站图片抓取小爬虫
- Java简单实现爬虫技术,抓取整个整个网站所有链接+图片+文件(思路+代码)
- 【python】100行代码python爬虫程序,抓取网站图片存储本地(附:中文链接解决)
- python爬虫 抓取一个网站的所有网址链接
- Python爬虫之从网站图片中抓取文字
- Python使用代理抓取网站图片(多线程)
- Python urllib2递归抓取某个网站下图片