使用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('爬取完成!!!!!')