简体   繁体   中英

Why does python return an error when using urllib.request.urlopen for certain websites

when I use url lib.requests in python 3, it works for some websites but for others it returns an error:

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'
>>> 

You need to change url to:

http://wikipedia.org

It should work now.

Try this :

import urllib.request
urllib.request.urlopen('https://www.wikipedia.org/')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM