马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 一个账号 于 2020-3-6 22:00 编辑
学习了小乌龟爬煎蛋网的教程,网站现在的网页名编排方式改了,不能直接用小乌龟的程序,作了点修改。新手代码打得不是很简洁明了,放上来交流学习吧。
import urllib.request
import os
import random
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/70.0.3538.25 Safari/537.36 Core/1.70.3722.400 QQBrowser/10.5.3751.400')
response = urllib.request.urlopen(url)
html = response.read()
return html
def get_page(url):
url_temp=url+'MjAyMDAzMDMt'+add_name[0]+'#comments'
print(url_temp)
html=url_open(url_temp).decode('utf-8')
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt') + 28
b=html.find('#',a)
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt',b) + 28
b=html.find('#',a)
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt',b) + 28
b=html.find('#',a)
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt',b) + 28
b=html.find('#',a)
add_name.append(html[a:b])
for i in range(pages-2):
url_temp=url+'MjAyMDAzMDMt'+add_name[i+1]+'#comments'
print(url_temp)
html=url_open(url_temp).decode('utf-8')
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt') + 28
b=html.find('#',a)
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt',b) + 28
b=html.find('#',a)
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt',b) + 28
b=html.find('#',a)
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt',b) + 28
b=html.find('#',a)
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt',b) + 28
b=html.find('#',a)
add_name.append(html[a:b])
return url_temp
def imgs_find(url):
for each in iter(add_name):
page_url=url+'MjAyMDAzMDMt'+each+'#comment'
html2=url_open(page_url).decode('utf-8')
a = html2.find('img src=')
while a != -1:
b = html2.find('.jpg',a,a+255)
if b != -1:
img_addrs.append(html2[a+9:b+4])
else:
b = a+9
a = html2.find('img src=',b)
def save_imgs(folder,img_addrs):
for each in img_addrs:
filename = each.split('/')[-1]
with open(filename,'wb') as f:
address = 'http:'+each
img = url_open(address)
f.write(img)
folder=input('请输入保存文件名:')
pages=int(input('请输入下载页数(大于等于3页):'))
try:
os.mkdir(folder)
except OSError as reason:
print(OSError)
finally:
os.chdir(folder)
add_name = []
img_addrs = []
url = 'http://jandan.net/ooxx/'
html=url_open(url).decode('utf-8')
a=html.find('jandan.net/ooxx/MjAyMDAzMDMt') + 28
b=html.find('#',a)
add_name.append(html[a:b])
get_page(url)
print(add_name)
imgs_find(url)
save_imgs('ooxx',img_addrs)
|