imcaimimi 发表于 2020-12-30 23:22:03

论一只爬虫的自我修养实战4-OOXX爬妹子图学习作品

2020.12.30,23:18
真正0基础学了45天到这儿,煎蛋网代码变了好多,网址需要解码,尝试了一下
import部分我也不知道2、3两个用不用,反正写上了……
import urllib.request
import urllib.parse
import json
import time as t
import base64

def open_url(url_in):
    req = urllib.request.Request(url_in)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66')
    response = urllib.request.urlopen(req)
    html_out = response.read()
    return html_out


def page_url(page):
    page_str = str(t.localtime().tm_year) + str(t.localtime().tm_mon) + str(t.localtime().tm_mday)+'-'+str(page)
    temp = str(base64.b64encode(str.encode(page_str)))
    temp = temp
    page_url = 'http://jandan.net/ooxx/'+ temp +'#comments'
    #print(page_url) #测试
    return page_url

def mm_page_url(page_url):
    mm_page_urls = []

    html = open_url(page_url)
    html = html.decode('utf-8')
   
    a = html.find('img src=')
    while a != -1:
      b = html.find('.jpg',a,a+100) #页面中图片地址字符长度57,小于100
      if b != -1:
            temp = 'http:'+ html[(a+9):(b+4)]
            #print(temp) #测试
            mm_page_urls.append(temp)
      else:
            break #小甲鱼用b = a+9
      a = html.find('img src=',b)
    #print(mm_page_urls) #测试
    return mm_page_urls
   

def mm_download(page):
    global num
    #page_urls = 'start'
    page_urls = page_url(page)
    #mm_page_url = []
    mm_page_url_out = mm_page_url(page_urls)
    for each in mm_page_url_out:
      mm_img = open_url(each)
      with open('C:\\Users\\imcaimimi\\Desktop\\python learning\\mm\\mm_%s.jpg' % (num),'wb') as f:
            f.write(mm_img)
      num +=1
      t.sleep(1)

n = int(input('Confirm the pages of mm_pics_jiandansuishoupai:'))
html = open_url('http://jandan.net/ooxx')
html = html.decode('utf-8')
ini_a = html.find('current-comment-page')
ini_b = html.find(']',ini_a)
ini_page = int(html)
#print(ini_page)#测试
num = 0
for i in range(n):
    mm_download(ini_page-i)

imcaimimi 发表于 2020-12-30 23:22:55

第一次发帖,请高手们多指教,哪些地方还能精简或者改进
页: [1]
查看完整版本: 论一只爬虫的自我修养实战4-OOXX爬妹子图学习作品