爬取电脑美女壁纸图片
import urllib.requestimport os
import re
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0')
# proxies =
response = urllib.request.urlopen(url)
html = response.read()
print('url',url)
return html
def get_page(url):
html = url_open(url).decode('GBK')
p = r'<a href="/bz/nxxz/shxz/(\d+)\.html" target="_blank">'
# 待会用正则
a = re.findall(p,html)
# b = a.find('shxz/')
# c = a.find('.html')
# a = html.find('<li><a href=')
# b = html.find('target=\"_blank\">',a)
print('正则提取的尾号',a)
# return html
return a
def find_imgs(url):
html = url_open(url).decode('GBK')
img_addrs = []
p = r'img id="bigImg" src="(.+\.jpg)"'
a = re.findall(p,html)
for i in a:
img_addrs.append(i)
# a = html.find('\"bigImg\" src')
# while a != -1:
# b = html.find('jpg',a,100)
# if b !=-1:
# img_addrs.append(html)
# else :
# b = a + 14
# a = html.find('\"bigImg\" src',b)
for each in img_addrs:
print('具体网址',each)
return img_addrs
def save_imgs(folder,img_addrs):
for each in img_addrs:
filename = each.split('/')[-1]
with open(filename,'wb') as f:
img = url_open(each)
f.write(img)
def My_ll(url,i):
html = url_open(url).decode('GBK')
q = r'<a href="/bz/nxxz/shxz/%d.html">[\u4e00-\u9fa5]+</a>\((\d+)[\u4e00-\u9fa5]\)'%(int(i))
fie = re.findall(q,html)
for ss in fie:
fie = int(ss)
print('这组有几张',fie)
return fie
def download_mm(folder='XXOO',pages=10):
os.mkdir(folder)
os.chdir(folder)
url = 'http://www.jj20.com/bz/nxxz/shxz/'
x = get_page(url)
for i in x:
print('插入URL的尾号',i)
page_num = i # 大尾号
dudu = My_ll(url,i)
if dudu != []:
for ll in range(1,dudu+1):
if ll == 1:
page_url = url + page_num + '.html'
print('page_url0',page_url)
else :
page_url = url + page_num + '_' + str(ll) + '.html'
print('page_url1-',page_url)
img_addrs = find_imgs(page_url)
save_imgs(folder,img_addrs)
else:
print('所有下载已完成!')
if __name__ == '__main__' :
download_mm()
学习
页:
[1]