欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python使用代理抓取网站图片(多线程)

程序员文章站 2022-12-28 11:52:43
一、功能说明:1. 多线程方式抓取代理服务器,并多线程验证代理服务器ps 代理服务器是从http://www.cnproxy.com/ (测试只选择了8个页面)抓取2. 抓...

一、功能说明:
1. 多线程方式抓取代理服务器,并多线程验证代理服务器
ps 代理服务器是从http://www.cnproxy.com/ (测试只选择了8个页面)抓取
2. 抓取一个网站的图片地址,多线程随机取一个代理服务器下载图片
二、实现代码

复制代码 代码如下:

#!/usr/bin/env python
#coding:utf-8

import urllib2
import re
import threading
import time
import random

rawproxylist = []
checkedproxylist = []
imgurl_list = []

#抓取代理网站
portdicts ={'v':"3",'m':"4",'a':"2",'l':"9",'q':"0",'b':"5",'i':"7",'w':"6",'r':"8",'c':"1"}
targets = []
for i in xrange(1,9):
        target = r"http://www.cnproxy.com/proxy%d.html" % i
        targets.append(target)
#print targets

#抓取代理服务器正则
p = re.compile(r'''<tr><td>(.+?)<script type=text/javascript>document.write\(":"\+(.+?)\)</script></td><td>(.+?)</td><td>.+?</td><td>(.+?)</td></tr>''')

#获取代理的类
class proxyget(threading.thread):
    def __init__(self,target):
        threading.thread.__init__(self)
        self.target = target

    def getproxy(self):
        print "代理服务器目标网站: " + self.target
        req = urllib2.urlopen(self.target)
        result = req.read()
        #print chardet.detect(result)
        matchs = p.findall(result)
        for row in matchs:
            ip=row[0]
            port =row[1]
            port = map(lambda x:portdicts[x],port.split('+'))
            port = ''.join(port)
            agent = row[2]
            addr = row[3].decode("cp936").encode("utf-8")
            proxy = [ip,port,addr]
            #print proxy
            rawproxylist.append(proxy)

    def run(self):
        self.getproxy()

#检验代理的类
class proxycheck(threading.thread):
    def __init__(self,proxylist):
        threading.thread.__init__(self)
        self.proxylist = proxylist
        self.timeout = 5
        self.testurl = "http://www.baidu.com/"
        self.teststr = "030173"

    def checkproxy(self):
        cookies = urllib2.httpcookieprocessor()
        for proxy in self.proxylist:
            proxyhandler = urllib2.proxyhandler({"http" : r'http://%s:%s' %(proxy[0],proxy[1])})
            #print r'http://%s:%s' %(proxy[0],proxy[1])
            opener = urllib2.build_opener(cookies,proxyhandler)
            opener.addheaders = [('user-agent', 'mozilla/5.0 (windows nt 6.2; wow64; rv:22.0) gecko/20100101 firefox/22.0')]
            #urllib2.install_opener(opener)
            t1 = time.time()

            try:
                #req = urllib2.urlopen("http://www.baidu.com", timeout=self.timeout)
                req = opener.open(self.testurl, timeout=self.timeout)
                #print "urlopen is ok...."
                result = req.read()
                #print "read html...."
                timeused = time.time() - t1
                pos = result.find(self.teststr)
                #print "pos is %s" %pos

                if pos > 1:
                    checkedproxylist.append((proxy[0],proxy[1],proxy[2],timeused))
                    #print "ok ip: %s %s %s %s" %(proxy[0],proxy[1],proxy[2],timeused)
                else:
                     continue
            except exception,e:
                #print e.message
                continue

    def run(self):
        self.checkproxy()

#获取图片地址函数
def imgurllist(url_home):
    global imgurl_list
    home_page = urllib2.urlopen(url_home)
    url_re = re.compile(r'<li><a href="(.+?)" target="_blank" rel="nofollow">')
    pic_re = re.compile(r'<img src="(.*?\.\w{3,4})"')
    url_list = re.findall(url_re,home_page.read())
    for url in url_list:
        #print url_home+url
        url_page = urllib2.urlopen(url_home+url)
        for imgurllist in re.findall(pic_re,url_page.read()):
            imgurl_list.append(imgurllist)

#下载图片的类
class getpic(threading.thread):
    def __init__(self,imgurl_list):
        threading.thread.__init__(self)
        self.imgurl_list = imgurl_list
        self.timeout = 5
    def downloadimg(self):
        for imgurl in self.imgurl_list:
            pic_suffix = imgurl.split('.')[-1] #获取图片后缀
            pic_name = str(random.randint(0,10000000000))+'.'+pic_suffix
            cookies = urllib2.httpcookieprocessor()
            randomcheckedproxy = random.choice(checkedproxylist) #随机取一组代理服务器
            proxyhandler = urllib2.proxyhandler({"http" : r'http://%s:%s' %(randomcheckedproxy[0],randomcheckedproxy[1])})
            opener = urllib2.build_opener(cookies,proxyhandler)
            opener.addheaders = [('user-agent', 'mozilla/5.0 (windows nt 6.2; wow64; rv:22.0) gecko/20100101 firefox/22.0')]
            urllib2.install_opener(opener)
            try:
                data_img = opener.open(imgurl,timeout=self.timeout)
                f = open (pic_name,'wb')
                f.write(data_img.read())
                f.close()
            except:
                continue
    def run(self):
        self.downloadimg()

if __name__ == "__main__":
    getthreads = []
    checkthreads = []
    imgurllist('http://www.ivsky.com')
    getpicthreads = []

#对每个目标网站开启一个线程负责抓取代理
for i in range(len(targets)):
    t = proxyget(targets[i])
    getthreads.append(t)

for i in range(len(getthreads)):
    getthreads[i].start()

for i in range(len(getthreads)):
    getthreads[i].join()

print '.'*10+"总共抓取了%s个代理" %len(rawproxylist) +'.'*10

#开启20个线程负责校验,将抓取到的代理分成20份,每个线程校验一份
for i in range(20):
    t = proxycheck(rawproxylist[((len(rawproxylist)+19)/20) * i:((len(rawproxylist)+19)/20) * (i+1)])
    checkthreads.append(t)

for i in range(len(checkthreads)):
    checkthreads[i].start()

for i in range(len(checkthreads)):
    checkthreads[i].join()

print '.'*10+"总共有%s个代理通过校验" %len(checkedproxylist) +'.'*10

#开启20个线程随机取一个代理下载图片
for i in range(20):
    t = getpic(imgurl_list[((len(imgurl_list)+19)/20) * i:((len(imgurl_list)+19)/20) * (i+1)])
    getpicthreads.append(t)

for i in range(len(getpicthreads)):
    getpicthreads[i].start()

for i in range(len(getpicthreads)):
    getpicthreads[i].join()

print '.'*10+"总共有%s个图片下载" %len(imgurl_list) +'.'*10

#代理排序持久化
f= open("proxy_list.txt",'w+')
for proxy in sorted(checkedproxylist,cmp=lambda x,y:cmp(x[3],y[3])):
    #print "checked proxy is: %s:%s\t%s\t%s" %(proxy[0],proxy[1],proxy[2],proxy[3])
    f.write("%s:%s\t%s\t%s\n"%(proxy[0],proxy[1],proxy[2],proxy[3]))
f.close()

二、测试结果:

复制代码 代码如下:

# ls
proxy_getpic.py
# python proxy_getpic.py
代理服务器目标网站: http://www.cnproxy.com/proxy1.html
代理服务器目标网站: http://www.cnproxy.com/proxy2.html
代理服务器目标网站: http://www.cnproxy.com/proxy3.html
代理服务器目标网站: http://www.cnproxy.com/proxy4.html
代理服务器目标网站: http://www.cnproxy.com/proxy5.html
代理服务器目标网站: http://www.cnproxy.com/proxy6.html
代理服务器目标网站: http://www.cnproxy.com/proxy7.html
代理服务器目标网站: http://www.cnproxy.com/proxy8.html
..........总共抓取了800个代理..........
..........总共有458个代理通过校验..........
..........总共有154个图片下载..........
# cat proxy_list.txt | more
173.213.113.111:3128    united states   0.432188987732
173.213.113.111:8089    united states   0.441318035126
173.213.113.111:7808    united states   0.444597005844
110.4.24.170:80 香港 香港移动通讯有限公司       0.489440202713
211.142.236.135:8080    湖南省株洲市 移动       0.490673780441
211.142.236.135:8081    湖南省株洲市 移动       0.518096923828
211.142.236.135:8000    湖南省株洲市 移动       0.51860499382
211.142.236.135:8082    湖南省株洲市 移动       0.520448207855
# ls
1001117689.jpg  3097883176.jpg  5234319709.jpg  7012274766.jpg  8504924248.jpg
1076458640.jpg  3144369522.jpg  5387877704.jpg  7106183143.jpg  867723868.jpg
1198548712.jpg  3161307031.jpg  5572092752.jpg  7361254661.jpg  8746315373.jpg
165738192.jpg   3228008315.jpg  5575388077.jpg  7389537793.jpg  8848973192.jpg
1704512138.jpg  3306931164.jpg  5610740708.jpg  7407358698.jpg  8973834958.jpg
1742167711.jpg  3320152673.jpg  5717429022.jpg  7561176207.jpg  8976862152.jpg
...............