未知python运行错误
  • 板块学术版
  • 楼主uFTvL9
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/3/7 23:18
  • 上次更新2023/10/28 07:03:29
查看原帖
未知python运行错误
411963
uFTvL9楼主2022/3/7 23:18

源代码:

#! python3
# This is  the CORRECTED code from the book.

import requests
import os
import bs4

url = 'http://xkcd.com'  # starting url
os.makedirs('xkcd', exist_ok=True)
while not url.endswith('#'):
    # Download the page
    print('Downloading page %s' % url)
    res = requests.get(url)
    res.raise_for_status()

    soup = bs4.BeautifulSoup(res.text)
    # Find the URL of the comic image.
    comicElem = soup.select('#comic img')
    if not comicElem:
        print("Could not find comic image")
    else:
        comicUrl = comicElem[0].get('src')
        # Download the image
        print('Downloading image %s...' % comicUrl)
        res = requests.get(comicUrl)
        res.raise_for_status()
        # Save image to ./xkcd.
        imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')
        for chunk in res.iter_content(100000):
            imageFile.write(chunk)
        imageFile.close()

    # Get the Prev button's url.
    prevLinc = soup.select('a[rel="prev"]')[0]
    url = f'http://xkcd.com{prevLinc.get("href")}'
print('Done')

错误信息:

C:\Users\HUAWEI\AppData\Local\Programs\Python\Python39\python.exe "F:/GATE/main - 副本.py"
Downloading page http://xkcd.com
F:\GATE\main - 副本.py:16: GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 16 of the file F:\GATE\main - 副本.py. To get rid of this warning, pass the additional argument 'features="html.parser"' to the BeautifulSoup constructor.

  soup = bs4.BeautifulSoup(res.text)
Traceback (most recent call last):
  File "F:\GATE\main - 副本.py", line 25, in <module>
    res = requests.get(comicUrl)
  File "C:\Users\HUAWEI\AppData\Roaming\Python\Python39\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\HUAWEI\AppData\Roaming\Python\Python39\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\HUAWEI\AppData\Roaming\Python\Python39\site-packages\requests\sessions.py", line 515, in request
    prep = self.prepare_request(req)
  File "C:\Users\HUAWEI\AppData\Roaming\Python\Python39\site-packages\requests\sessions.py", line 443, in prepare_request
    p.prepare(
  File "C:\Users\HUAWEI\AppData\Roaming\Python\Python39\site-packages\requests\models.py", line 318, in prepare
    self.prepare_url(url, params)
  File "C:\Users\HUAWEI\AppData\Roaming\Python\Python39\site-packages\requests\models.py", line 392, in prepare_url
    raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL '//imgs.xkcd.com/comics/outlet_denier.png': No scheme supplied. Perhaps you meant http:////imgs.xkcd.com/comics/outlet_denier.png?
Downloading image //imgs.xkcd.com/comics/outlet_denier.png...

进程已结束,退出代码1

2022/3/7 23:18
加载中...