马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 有谁懂 于 2020-5-14 22:48 编辑
按照旧版教程,终于爬到煎蛋的妹妹图了!
但由于新的煎蛋网站改变了page的格式以及图片url缺少http:,可能有的鱼油按老师的方法爬不到。
我把自己的源码给大家分享下,注意就是解决上面2个问题。
希望大家都能爬到自己喜欢的妹妹!import urllib.request
import os
import base64
import time
#读取网站源码
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/81.0.4044.138 Safari/537.36')
response = urllib.request.urlopen(req)
html = response.read()
return html
#读取页码url
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]
#读取图片url
def find_imgs(url):
html = url_open(url).decode('utf-8')
imgs_addrs = []
a = html.find('img src=')
while a != -1:
b = html.find('.jpg', a, a+255)
if b != -1:
imgs_addrs.append("http:" + html[a+9:b+4])
else:
b = a + 9
a = html.find('img src=', b)
return imgs_addrs
#保存图片
def save_imgs(folder, img_addrs):
i = 0
for each in img_addrs:
i += 1
filename = each.split('/')[-1]
with open(filename, 'wb') as f:
img = url_open(each)
f.write(img)
#将页码和日期加密为base64
def de_page(page_num):
page_num = '20' + time.strftime('%y%m%d') + '-' + str(page_num)
result = base64.b64encode(page_num.encode())
result = result.decode('utf_8')
return result
#爬取前pages页的.jpg文件
def download_mm(folder='ooxx', pages=10):
os.mkdir(folder)
os.chdir(folder)
url = 'http://jandan.net/ooxx/'
page_num = int(get_page(url))
for each in range(pages):
page_num -= each
t_page = de_page(page_num)
page_url = url + '%s#comments' % t_page
img_addrs = find_imgs(page_url)
save_imgs(folder, img_addrs)
print(page_url)
if __name__ == '__main__':
download_mm()
|