计算机界卷王 发表于 2021-10-19 15:20:59

报错了,如何解决?

import urllib.request
import os


def url_open(url):
    req = urllib.request.Request(url)
    req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
    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


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)
      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 downlode_mm(folder="OOXX",pages=10):
    os.mkdir(folder)
    os.chdir(folder)

    url = "http://jandan.net/girl/"

    page_num = int(get_page(url))

    for i in range(pages):
      page_num -= i
      page_url = url + str(page_num) + "#comments"
      img_addrs = find_imgs(page_url)
      save_imgs(folder,img_addrs)
      
if __name__ == "__main__":
    downlode_mm()





Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/python实例/pachong.py", line 69, in <module>
    downlode_mm()
File "C:/Users/Administrator/Desktop/python实例/pachong.py", line 66, in downlode_mm
    save_imgs(folder,img_addrs)
File "C:/Users/Administrator/Desktop/python实例/pachong.py", line 49, in save_imgs
    img = url_open(each)
File "C:/Users/Administrator/Desktop/python实例/pachong.py", line 6, in url_open
    req = urllib.request.Request(url)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 328, in __init__
    self.full_url = url
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 354, in full_url
    self._parse()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 383, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '//wx3.sinaimg.cn/mw600/0076BSS5ly8gvkn0gfpsij60u011ijyi02.jpg'

suchocolate 发表于 2021-10-19 20:26:19

本帖最后由 suchocolate 于 2021-10-20 08:15 编辑

ValueError: unknown url type: '//wx3.sinaimg.cn/mw600/0076BSS5ly8gvkn0gfpsij60u011ijyi02.jpg'
核对一下你的url获取方式,url要用“http:”或“https:”开头。


别人的案例:
import requests
import os
import re


def main():
    dir_name = 'pics'   # 存储图片
    if not os.path.exists(dir_name):# 检查图片存储路径
      os.mkdir(dir_name)
    os.chdir(dir_name)
    # num = int(input('请输入想下载的页数:'))
    num = 3
    url = 'http://jandan.net/girl'
    headers = {'user-agent': 'firefox'}
    result = []   # 存储图片的url
    r = requests.get(url, headers=headers)
    nx_page = re.findall(r'Comments" href="(.*?)"', r.text)   # 找到下一页的url
    for item in range(num):
      r = requests.get('http:' + nx_page, headers=headers)
      result.extend(re.findall(r'<img src="(.*?)"', r.text))   # 把当前页面图片的url存到result
      nx_page = re.findall(r'Comments" href="(.*?)"', r.text)# 找到下一页的url
    print(f'总共{len(result)}张图片')
    counter = 1
    for item in result:
      pic_name = item.split('/')[-1]
      try:
            r = requests.get('http:' + item, headers=headers, timeout=5)
      except Exception as e:
            print(e)
      with open(pic_name, 'wb') as f:
            f.write(r.content)
            print(f'已下载{pic_name}, 共下载{counter}。')
            counter += 1


if __name__ == '__main__':
    main()
页: [1]
查看完整版本: 报错了,如何解决?