Python3 爬虫实战(二)——图片爬虫

来源:互联网 发布:渐变颜色搭配的数据 编辑:程序博客网 时间:2024/05/29 07:17

   上一篇博文http://blog.csdn.net/nju_flepped/article/details/75452517爬取了ONE的每日一句,ONE不仅每日一句都很经典,每天的图片也都很好看。这次我们就来爬取每期的图片(截止到2017年7月19号)。
   有了上一次爬虫的基础,这次要轻松很多。我们这次只需要分析页面源代码找到目标图片所在的标签即可。源代码如下:

页面源代码

   通过观察源代码我们可以看到,目标图片所在的标签是img标签,我们只需要使用bs4的find_all()查找函数,即可找到,整个源代码共有两个img标签,目标图片在第二个img标签中(所以第22行代码中用h[1]取第二个img标签)。代码如下:

import refrom urllib import requestimport requestsfrom bs4 import BeautifulSoupurl='http://wufazhuce.com/one/'#每一期公共部分Path='B:\\pytest\\MLtest\\one_img\\'#图片保存路径num=0#记录爬取照片的个数for i in range(14,1775):    s=str(i)    currenturl=url+s#当前期的url    try:        res=requests.get(currenturl)        res.raise_for_status()    except requests.RequestException as e:        print(e)    else:        html=res.text        soup = BeautifulSoup(html,'html.parser')        a=soup.select('.one-titulo')#期次        h=soup.find_all('img')#图片标签        imgUrl=h[1].get('src')#取图片的链接        index=re.sub("\D","",a[0].string.split()[0])#取得期次        if(index==''):            continue        imgName=Path+'VOL.'+index+'.jpg'#图片的完整路径含图片名        request.urlretrieve(imgUrl,imgName)#保存图片        num+=1        print('已爬取%s张图片...'%num)print('-----爬取结束-----')

结果:
这里写图片描述

原创粉丝点击