鱼C论坛

 找回密码
 立即注册
查看: 1039|回复: 1

[已解决]爬取煎蛋妹子图求助

[复制链接]
发表于 2020-8-11 22:00:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import urllib.request
import os
import base64
import re

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/84.0.4147.105 Safari/537.36')
    response = urllib.request.urlopen(req)
    html = response.read()
    print(html)
   
    return html

   
def get_page(url):
    html = url_open(url).decode('utf-8')

    a = r'<span class="current-comment-page">\[(\d+)\]</span>'
    page_list = re.findall(a,html)

    for each in page_list:
        t = each

        return t

      
def base_num(page_num):
    times = '20200811-' + str(page_num)
    s = times.encode('utf-8')
    sw = base64.b64encode(s)
    str_sw = str(sw)
    string = str_sw.split("'")[-1]
   
    return string


def find_pic_url(url):
    html = url_open(url).decode('utf-8')
    pic_url = []


    a = r'<img src="([^"]+\.jpg)"'
    b = re.findall(a,html)
   
    if not len(b):
        pic_url.append(b)

    return pic_url

        
def save_pic(folder, pic_url):
    for each in pic_url:
        filename = each.split('/')[-1]
        with open(filename, 'wb') as f:
            img = open_url(each)
            f.write(img)

def download_mm(folder = 'mm', pages = 10):
    #创建一个文件夹
    os.mkdir(folder)
    os.chdir(folder)

    url = 'http://jandan.net/ooxx/'
    #获取页码
    page_num = int(get_page(url))

    for i in range(pages):
        page_num -=i
        #base64加密
        x = base_num(page_num)
        #获取页码地址
        page_url = url + x + '#comments'
        #获取图片具体地址并保存成列表
        pic_url = find_pic_url(page_url)
        save_pic(folder, pic_url)

        
if __name__ == '__main__':
    download_mm()





我这个代码哪里出了错误 导致下载不下来图片呢
最佳答案
2020-8-11 22:36:56

帮你改完了,代码中一个函数名拼错了,还有加密的地方有点错误

以及获取图片 url 时候,你 if 判断导致返回的永远是 pic_url = [] 空列表了
import urllib.request
import os
import base64
import re



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/84.0.4147.105 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 = r'<span class="current-comment-page">\[(\d+)\]</span>'
    page_list = re.findall(a, html)

    for each in page_list:
        t = each

        return t


def base_num(page_num):
    page_num = '20200811-'+ str(page_num)
    result = base64.b64encode(page_num.encode())
    result = result.decode('utf_8')

    return result


def find_pic_url(url):
    html = url_open(url).decode('utf-8')

    a = r'<img src="([^"]+\.jpg)"'
    pic_url = re.findall(a, html)

    return pic_url


def save_pic(pic_url):
    for each in pic_url:
        filename = each.split('/')[-1]
        with open(filename, 'wb') as f:
            img = url_open('http:'+each)
            f.write(img)


def download_mm(folder='mm', pages=10):
    # 创建一个文件夹
    os.mkdir(folder)
    os.chdir(folder)

    url = 'http://jandan.net/ooxx/'
    # 获取页码
    page_num = int(get_page(url))

    for i in range(pages):
        page_num -= i
        # base64加密
        x = base_num(page_num)
        # 获取页码地址
        page_url = url + x + '#comments'
        # 获取图片具体地址并保存成列表
        pic_url = find_pic_url(page_url)
        save_pic(pic_url)


if __name__ == '__main__':
    download_mm()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-11 22:36:56 | 显示全部楼层    本楼为最佳答案   

帮你改完了,代码中一个函数名拼错了,还有加密的地方有点错误

以及获取图片 url 时候,你 if 判断导致返回的永远是 pic_url = [] 空列表了
import urllib.request
import os
import base64
import re



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/84.0.4147.105 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 = r'<span class="current-comment-page">\[(\d+)\]</span>'
    page_list = re.findall(a, html)

    for each in page_list:
        t = each

        return t


def base_num(page_num):
    page_num = '20200811-'+ str(page_num)
    result = base64.b64encode(page_num.encode())
    result = result.decode('utf_8')

    return result


def find_pic_url(url):
    html = url_open(url).decode('utf-8')

    a = r'<img src="([^"]+\.jpg)"'
    pic_url = re.findall(a, html)

    return pic_url


def save_pic(pic_url):
    for each in pic_url:
        filename = each.split('/')[-1]
        with open(filename, 'wb') as f:
            img = url_open('http:'+each)
            f.write(img)


def download_mm(folder='mm', pages=10):
    # 创建一个文件夹
    os.mkdir(folder)
    os.chdir(folder)

    url = 'http://jandan.net/ooxx/'
    # 获取页码
    page_num = int(get_page(url))

    for i in range(pages):
        page_num -= i
        # base64加密
        x = base_num(page_num)
        # 获取页码地址
        page_url = url + x + '#comments'
        # 获取图片具体地址并保存成列表
        pic_url = find_pic_url(page_url)
        save_pic(pic_url)


if __name__ == '__main__':
    download_mm()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-19 11:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表