妹子图简单爬取

使用urllib库 和正则表达式 进行的简单图片爬取,由于还不是特别精通,有些过程处理的不尽人意…
妹子图网址

'''
    注:别忘了将file的路径重新设置
    如果在自己电脑不行,可以自行加代理,或者延时一下
'''

import urllib.request
import re
#import time
import urllib.error
# ip = ['122.193.247.96','112.87.68.114','59.37.18.243','121.17.174.121']    #代理
# type = ['HTTPS','HTTP','HTTPS','HTTP']
# proxy_suppoer = urllib.request.ProxyHandler({ip[0]:type[0]})
# opener = urllib.request.build_opener(proxy_suppoer)
# urllib.request.install_opener(opener)
headers = {'Users-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
url = 'https://www.xiuaa.com/'
req = urllib.request.Request(url,headers=headers)
html = urllib.request.urlopen(req).read().decode("utf-8")
pat = '<a href="(.*?).html" title='
data = re.compile(pat).findall(html)
for i in range(0,len(data),2):
    for j in range(30):
        try:
            url_t = 'https://www.xiuaa.com/' + data[i] + '_' + str(j) + '.html'
            req_t = urllib.request.Request(url_t,headers=headers)
            html_t = urllib.request.urlopen(req_t).read().decode("utf-8")
            pat = '<img alt="(.*?)" src="(.*?)">'
            data_t = re.compile(pat,re.S).findall(html_t)
            data_f = list(data_t[0])
            print("正在爬取{}个第{}个图片...".format(i,j))
            if data_f[1][0] != 'h':
                index = data_f[1].find('g')
                url = "https://www.xiuaa.com" + data_f[1][0:index+1]
            else:
                index= data_f[1].find('jpg')
                url = data_f[1][0:index+3]
            reponse = urllib.request.urlopen(url).read()
            file = 'C:\\Users\hxy\Desktop\课题\妹子图\\' + data_f[0] + ".jpg"
            with open(file,'wb') as f:
                f.write(reponse)
           #time.sleep(0.5)    #延时
            print("爬取成功!")
        except urllib.error.URLError as e:
            print(e.reason)

print('爬取完成!!!!!')
print('爬取完成!!!!!')
print('爬取完成!!!!!')
print('爬取完成!!!!!')
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值