鱼C论坛

 找回密码
 立即注册
查看: 501|回复: 2

新手,学习小甲鱼论一个爬虫的自我修养4,运行报错,求解!

[复制链接]
发表于 2018-10-14 17:36:18 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 miladuo 于 2018-10-14 18:06 编辑

import urllib.request
import os

def url_open(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.102 Safari/537.36 Vivaldi/2.0.1309.29')
    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') + 23
    b = html.find(']', a)

    return html[a:b]

def find_imgs(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(folder, 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='OOXX', 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
        page_url = url + 'page-' + str(page_num) + '#comments'
        img_addrs = find_imgs(page_url)
        save_imgs(folder, img_addrs)

if __name__ == '__main__':
    download_mm()



报错信息如下:
Traceback (most recent call last):
  File "/Users/jinxin/Library/Mobile Documents/com~apple~CloudDocs/Python/Python网络爬虫与信息提取/小甲鱼爬虫部分/4.download_mm.py", line 78, in <module>
    download_mm()
  File "/Users/jinxin/Library/Mobile Documents/com~apple~CloudDocs/Python/Python网络爬虫与信息提取/小甲鱼爬虫部分/4.download_mm.py", line 75, in download_mm
    save_imgs(folder, img_addrs)
  File "/Users/jinxin/Library/Mobile Documents/com~apple~CloudDocs/Python/Python网络爬虫与信息提取/小甲鱼爬虫部分/4.download_mm.py", line 48, in save_imgs
    img = url_open(each)
  File "/Users/jinxin/Library/Mobile Documents/com~apple~CloudDocs/Python/Python网络爬虫与信息提取/小甲鱼爬虫部分/4.download_mm.py", line 7, in url_open
    req = urllib.request.Request(url)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 329, in __init__
    self.full_url = url
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 355, in full_url
    self._parse()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 384, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '//img.jandan.net/news/2018/10/aad17b65b4475b19cf260506c766a79a.jpg'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-14 17:38:12 | 显示全部楼层
课程视频已经很老了,且煎蛋反扒了,需要升级你的爬虫手段
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-14 17:59:34 | 显示全部楼层
反扒

建议换个简单的网站练手
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-7 05:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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