Python爬虫实战之利用多线程爬取千图网的素材图片

千图网电商淘宝素材网址:https://www.58pic.com/piccate/3-0-0-p1.html

from  urllib import  request
import urllib
import random
from urllib.error import URLError
from urllib.request import ProxyHandler, build_opener
import re
def get_ip(): 
    fr=open('ip.txt','r')  ##代理IP池
    ips=fr.readlines()
    new=[]
    for line in ips:
        temp=line.strip()
        new.append(temp)
    ip=random.choice(new)
    return ip
    print(ip)
proxy =get_ip()
proxy_handler = ProxyHandler({
'http': 'http://' + proxy,
'https': 'https://' + proxy
})
opener = build_opener(proxy_handler)
import  threading
class One(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        try:
            for i in range(1,5,2):
                pageurl='http://www.58pic.com/piccate/3-0-0-p'+str(i)+'.html'
                data =urllib.request.urlopen(pageurl).read().decode('utf-8','ignore')
                pat='class="thumb-box".*?src="(.*?).jpg!'
                image_url=re.compile(pat).findall(data)  ##爬取高清图片部分地址
                for j in range(0,len(image_url)):
                    try:
                        this_list=image_url[j]
						#通过观察高请图片完整地址,添加完整地址
                        this_url=this_list+'.jpg!/fw/1024/watermark/url/L2ltYWdlcy93YXRlcm1hcmsveGlhb3R1LnBuZw==/align/center' 
                        file='D:/软件(学习)/Python/Test/chapter6/qiantu.photo/'+str(i)+str(j)+'.jpg'  #保存图片
                        urllib.request.urlretrieve(this_url,file)
                        print('第'+str(i)+'页第'+str(j)+'个图片成功')
                    except urllib.error.URLError as e:
                        print(e.reason)

        except URLError as e:
            print(e.reason)

采用多线程的方法

class Two(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        try:
            for i in range(2, 5, 2):
                pageurl = 'http://www.58pic.com/piccate/3-0-0-p'+str(i)+'.html'
                data = urllib.request.urlopen(pageurl).read().decode('utf-8', 'ignore')
                pat = 'class="thumb-box".*?src="(.*?).jpg!'
                image_url = re.compile(pat).findall(data)
                for j in range(0, len(image_url)):
                    try:
                        this_list = image_url[j]
                        this_url = this_list + '.jpg!/fw/1024/watermark/url/L2ltYWdlcy93YXRlcm1hcmsveGlhb3R1LnBuZw==/align/center'
                        file = 'D:/软件(学习)/Python/Test/chapter6/qiantu.photo/' + str(i) + str(j) + '.jpg'
                        urllib.request.urlretrieve(this_url, file)
                        print('第' + str(i) + '页第' + str(j) + '个图片成功')
                    except urllib.error.URLError as e:
                        print(e.reason)

        except URLError as e:
            print(e.reason)
one=One()
one.start()
two=Two()
two.start()

在这里插入图片描述
在这里插入图片描述
爬取成功!!!

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值