Home  >  Article  >  Backend Development  >  python小技巧之批量抓取美女图片

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

WBOY
WBOYOriginal
2016-06-06 11:30:28970browse

其中用到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'

', text, re.S)
    urls = re.finditer(r'

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

',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行,不是太难,大家可以研究下,这里我只是抛砖引玉,大家可以根据原理开发出其他的抓取程序,呵呵,自己想去吧。。。我就不多说了~~

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn