import urllib.request
import os
import base64
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36')
response = urllib.request.urlopen(req)
html = response.read()
return html
def find_imges(url):
html = url_open(url).decode('utf-8')
img_addrs = []
a = html.find('img src')
while a!=-1:
b = html.find('.jpg',a,a+255)
if b!=-1:
img_addrs.append('http:'+html[a+9:b+4])
else:
b=a+9
a = html.find('img src',b)
return img_addrs
def get_page(url):
html = url_open(url).decode('utf-8')
a= html.find('current-comment-page')+23
b =html.find(']',a)
print(html[a:b])
return html[a:b]
def save_imges(file,urls):
for each in urls:
filename = each.split('/')[-1]
with open(filename,'wb') as f:
img = url_open(each)
f.write(img)
def download_mm(file = "ooxx3",pagenum = 10):
os.mkdir(file)
os.chdir(file)
url = 'http://jandan.net/girl/'
# 首先访问到页面,查看页面内的页码
page_num = 65
# int(get_page(url))
# 其次根据页码得到一定量的页面的网址
for i in range(pagenum):
page_num-=1
datee = "20210726-"
str_url = datee+str(page_num)
encode_url = base64.encodebytes(str_url.encode('utf-8')).decode()
aa = "#com"
detail_url = url+encode_url
detail_ur =detail_url+aa
print(detail_ur)
# 然后对网址内的jpg进行网址识别抓取
img_addrs =find_imges(detail_url)
#然后对jpg进行保存到文件
save_imges(file,img_addrs)
download_mm()
# 代理使用 要得到代理表达式,获得代理支持urllib.request.proxyhander()
# 然后将获得build_oener() ,然后在安装到opener,urllib.request.intall_opener
by :参照小甲鱼视频