你的代码要改两处地方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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36')
response=urllib.request.urlopen(req)
html=response.read()
return html
def get_page(url):
html=url_open(url).decode('utf-8')
a=html.find('current-comment-page')+23
b=html.find(']',a)
return html[a:b]
def find_imgs(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(html[a+9:b+4])
else:
b=a+9
a=html.find('img src=',b)
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("http:" + each) # 第二处
f.write(img)
def download_mm(floder='ooxx',pages=10):
os.mkdir(floder)
os.chdir(floder)
url='http://jandan.net/girl'
page_num=int(get_page(url))
for i in range(pages):
s='20220122-'
page_num-=i
a=s+str(page_num)
i=base64.b64encode(a.encode())
page_url=url+'/'+i.decode()+'#comments'
img_addrs=find_imgs(page_url)
save_imgs(floder,img_addrs)
if __name__=='__main__':
download_mm()
|