|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
大佬们求助,照着小甲鱼的教程走的为啥跑不出来啊
import urllib.request
import os
def url_open(url):
req = urllib.request.Request(url)
req.add_header('user-agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')
response = urllib.request.urlopen(url)
html = response.read()
return html
def get_page(url):
html = url_open(url).decode('utf-8')
a = html.find('current_comment_page') + 10
b = html.find(']',a)
return html[a:b]
def find_img(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(floder, 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 download_mm(folder = 'jiandan', pages = 10):
os.mkdir(folder)
os.chdir(folder)
url = 'https://mil.ifeng.com/c/'
page_num = int(get_page(url))
for i in range(pages):
page_num -= i
page_url = url +'8GxxlRAQip' + str(page_num) +'#p=1'
img_addrs = find_imgs(page_url)
save_imgs(folder,img_addrs)
if __name__ == '__main__':
download_mm()
|
|