Python 爬取高清桌面壁纸
今天写了一个脚本用来爬取ZOL桌面壁纸网站的高清图片;
链接:http://desk.zol.com.cn/1920x1080/
本程序只爬了美女板块的图片,若要下载其他板块,只需修改程序中的”meinv“即可
代码如下:
#coding=utf-8
import urllib
import re
import time
class Spider:
baseUrl='http://desk.zol.com.cn/'
pic_index=0
itemGroupPic=[]
def __init__(self,page_count):
time.sleep(1)
url=self.baseUrl+"meinv/1920x1080/"
for i in range(10, page_count):
time.sleep(5)
html=self.getHtml(url,i)
getbi=self.getPageImageGroup(html)
bizhi_url=self.getbizhiurlList(getbi)
def getHtml(self,url,page_index):
url=url+str(page_index)+".html"
page = urllib.urlopen(url)
html = page.read()
return html
def getPageImageGroup(self,html):
reg=r'<a class="pic" href="/bizhi/.*?.html'
imgre=re.compile(reg)
imagelist=re.findall(imgre,html)
return imagelist
def getbizhiurlList(self,imagelist):
for iurl in imagelist:
reg=r'bizhi/.*?.html'
imgre=re.compile(reg)
itmeimageurl=re.findall(imgre,iurl)
self.itemGroupPic.append(itmeimageurl)
def GetCurrentUrlAndDownload(self,url):
page = urllib.urlopen(url)
html = page.read() #read()出来的文本和网页右键源代码有点出入,这里需要优化
reg=r'<img id="bigImg" src="http://.*.jpg"'
imgre=re.compile(reg)
urllist=re.findall(imgre,html)
for _u in urllist:
reg1=r'http://.*.jpg'
imgre1=re.compile(reg1)
itmeimageurl=re.findall(imgre1,_u)
print u'正在下载'+str(self.pic_index)+u'图片'
#D:\PictureAvi目录要事先创建好
urllib.urlretrieve(itmeimageurl[0],'D:\PictureAvi\%s.jpg' % self.pic_index)
self.pic_index+=1
#获取当前页面的url,next_html
next_reg=r'<a id="pageNext" class="next" href=".*.html"'
next_imgre=re.compile(next_reg)
next_urllist=re.findall(next_imgre,html)
if(len(next_urllist)==0):
return ""
#获取真正的next_html
next_reg_child=r'bizhi.*?.html'
next_imgre_child=re.compile(next_reg_child)
real_url=re.findall(next_imgre_child,next_urllist[0])
return real_url[0]
def MatchUrl(self,imagelist):
for imgurl in imagelist:
url=self.baseUrl+imgurl[0]
next_url=self.GetCurrentUrlAndDownload(url)
#递归获取下一个url
while(next_url != ''):
_itme_next_url=self.baseUrl+next_url
next_url=self.GetCurrentUrlAndDownload(_itme_next_url)
if __name__ == '__main__':
spider = Spider(15)
spider.MatchUrl(spider.itemGroupPic)
print u'结束下载'
运行结果如下:
运行环境是py2.7,原理很简单,并没有用到类似scrapy这样的框架,也没用什么动态获取
1.通过urllib获取网页源代码
2.找到规律,通过正则表达式找到url
3.用urllib下载到本地文件
未完待续。。。。。
Python 爬取高清桌面壁纸的更多相关文章
- Python 爬取 "王者荣耀.英雄壁纸" 过程中的矛和盾
1. 前言 学习爬虫,最好的方式就是自己编写爬虫程序. 爬取目标网站上的数据,理论上讲是简单的,无非就是分析页面中的资源链接.然后下载.最后保存. 但是在实施过程却会遇到一些阻碍. 很多网站为了阻止爬 ...
- python3爬取高清壁纸(1)
这次爬取的目标是:美桌网首页 > 桌面壁纸 > 卡通动漫 类别下的壁纸. 我们先随机选取一个专辑来爬(http://www.win4000.com/wallpaper_detail_545 ...
- python3爬取高清壁纸(2)
上次只是爬取一个专辑的图片,这次要爬取一整个页面的所有专辑的图片. 在上次的代码的基础上进行修改就行了,从专辑的索引页面开始,爬取该页面上所有的专辑的链接,再套用上次的代码就行了. 若要爬取多个页面只 ...
- python爬取高匿代理IP(再也不用担心会进小黑屋了)
为什么要用代理IP 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人 ...
- python 爬取王者荣耀高清壁纸
代码地址如下:http://www.demodashi.com/demo/13104.html 一.前言 打过王者的童鞋一般都会喜欢里边设计出来的英雄吧,特别想把王者荣耀的英雄的高清图片当成电脑桌面 ...
- 初识python 之 爬虫:爬取某网站的壁纸图片
用到的主要知识点:requests.get 获取网页HTMLetree.HTML 使用lxml解析器解析网页xpath 使用xpath获取网页标签信息.图片地址request.urlretrieve ...
- Python爬取视频指南
摘自:https://www.jianshu.com/p/9ca86becd86d 前言 前两天尔羽说让我爬一下菜鸟窝的教程视频,这次就跟大家来说说Python爬取视频的经验 正文 https://w ...
- Python 爬取所有51VOA网站的Learn a words文本及mp3音频
Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...
- python爬取网站数据
开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...
随机推荐
- 【转帖】WebRTC回声抵消模块简要分析
webrtc 的回声抵消(aec.aecm)算法主要包括以下几个重要模块:回声时延估计:NLMS(归一化最小均方自适应算法):NLP(非线性滤波):CNG(舒适噪声产生).一般经典aec算法还应包括双 ...
- shell学习笔记1: shell 中的变量与常见符号使用方法
变量 声明即用 a=2 b="123" 调用 ${varName}或者 $varName echo $b echo ${a} 常见变量 $?:判断上一个语句是否成功 $0:执行脚本 ...
- System.Web.Mvc.PartialViewResult.cs
ylbtech-System.Web.Mvc.PartialViewResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, ...
- typedef int (init_fnc_t) (void)和typedef int (*init_fnc_t) (void)
1.typedef int (init_fnc_t) (void);表示定义init_fnc_t为函数类型,该函数返回int型,无参数.而“init_fnc_t *init_sequence[]= ...
- Python使用微信接入图灵机器人
1.wxpy库介绍 wxpy 在 itchat 的基础上,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展. 文档地址:https://wxpy.readthedocs.io 从 PYPI 官 ...
- java基础温习 -- 多态
1. 基本概念 多态是指一个事物有不同的表现形式或形态. 多态存在的三个必要条件:要有继承.要有重写.父类变量引用子类对象. 当使用多态方式调用方法时: 首先检查父类中是否有该方法,如 ...
- css 实现头像周围光圈动态效果
效果: html文件: <img class="userHead" src="xx/user.jpg"> css文件: .userHead{ wid ...
- sprignboot 中thymeleaf和freemarker 都存在时,默认选择哪个
我们 无聊的时候想到,freemarker和thymeleaf都是springboot默认支持的模板,当这2个同时存在并有相同名字的时候,springboot会默认选择哪个模板来显示呢 ? 所以今天我 ...
- Centos7解决在同一局域网内无法使用ssh连接
参考: https://www.cnblogs.com/liyuanhong/articles/5785368.html 一.修改网卡设置 nano /etc/sysconfig/network-sc ...
- 新浪新闻API
新浪新闻API ustcmio 关注 2017.01.15 20:44* 字数 536 阅读 2479评论 2喜欢 7 新浪新闻的API:1.访问手机新浪网https://sina.cn/?from= ...