python小技巧之批量抓取美女图片

yipeiwu_com6年前Python爬虫

其中用到urllib2模块和正则表达式模块。下面直接上代码:

[/code]
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#通过urllib(2)模块下载网络内容
import urllib,urllib2,gevent
#引入正则表达式模块,时间模块
import re,time
from gevent import monkey

monkey.patch_all()

def geturllist(url):
    url_list=[]
    print url      
    s = urllib2.urlopen(url)
    text = s.read()
    #正则匹配,匹配其中的图片
    html = re.search(r'<ol.*</ol>', text, re.S)
    urls = re.finditer(r'<p><img src="(.+?)jpg" /></p>',html.group(),re.I)
    for i in urls:
        url=i.group(1).strip()+str("jpg")
        url_list.append(url)
    return url_list

def download(down_url):
    name=str(time.time())[:-3]+"_"+re.sub('.+?/','',down_url)
    print name
    urllib.urlretrieve(down_url, "D:\\TEMP\\"+name)

def getpageurl():
    page_list = []
    #进行列表页循环
    for page in range(1,700):
        url="http://jandan.net/ooxx/page-"+str(page)+"#comments"
        #把生成的url加入到page_list中
        page_list.append(url)
    print page_list
    return page_list
if __name__ == '__main__':
    jobs = []
    pageurl = getpageurl()[::-1]
    #进行图片下载
    for i in pageurl:
        for (downurl) in geturllist(i):
            jobs.append(gevent.spawn(download, downurl))
    gevent.joinall(jobs)
[/code]

程序不长才45行,不是太难,大家可以研究下,这里我只是抛砖引玉,大家可以根据原理开发出其他的抓取程序,呵呵,自己想去吧。。。我就不多说了~~

相关文章

零基础写python爬虫之抓取糗事百科代码分享

零基础写python爬虫之抓取糗事百科代码分享

项目内容: 用Python写的糗事百科的网络爬虫。 使用方法: 新建一个Bug.py文件,然后将代码复制到里面后,双击运行。 程序功能: 在命令提示行中浏览糗事百科。 原理解...

Python爬虫使用浏览器cookies:browsercookie过程解析

很多用Python的人可能都写过网络爬虫,自动化获取网络数据确实是一件令人愉悦的事情,而Python很好的帮助我们达到这种愉悦。然而,爬虫经常要碰到各种登录、验证的阻挠,让人灰心丧气(网...

Python编写百度贴吧的简单爬虫

操作:输入带分页的地址,去掉最后面的数字,设置一下起始页数和终点页数 功能:下载对应页码的所有页面并储存为HTML文件,以当前时间命名 代码: # -*- coding: utf-8...

Python 爬虫的工具列表大全

网络 通用 urllib -网络库(stdlib)。 requests -网络库。 grab...

Python下使用Scrapy爬取网页内容的实例

上周用了一周的时间学习了Python和Scrapy,实现了从0到1完整的网页爬虫实现。研究的时候很痛苦,但是很享受,做技术的嘛。 首先,安装Python,坑太多了,一个个爬。由于我是wi...