首页 > 解决方案 > 为什么python在某些网站使用urllib.request.urlopen时会返回错误

问题描述

当我在 python 3 中使用 url lib.requests 时,它适用于某些网站,但对于其他网站,它会返回错误:

import urllib.request
urllib.request.urlopen('wikipedia.org')

Traceback (most recent call last):
  File "/Users/etudiant/Desktop/test.py", line 3, in <module>
    urllib.request.urlopen('wikipedia.org')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 510, in open
    req = Request(fullurl, data)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 328, in __init__
    self.full_url = url
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 354, in full_url
    self._parse()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 383, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: 'wikipedia.org'
>>> 

标签: pythonpython-3.xurllib

解决方案


您需要将网址更改为:

http://wikipedia.org

它现在应该可以工作了。


推荐阅读