零基础学习python第53将课后习题求助
我是直接复制课后习题答案源代码的:import urllib.request
import chardet
def main():
url = input("请输入URL:")
response = urllib.request.urlopen(url)
html = response.read()
# 识别网页编码
encode = chardet.detect(html)['encoding']
if encode == 'GB2312':
encode = 'GBK'
print("该网页使用的编码是:%s" % encode)
if __name__ == "__main__":
main()
但运行后的结果却出现了这个问题:
请输入URL:www.douban.com
Traceback (most recent call last):
File "E:\文件\研究生\Python\程序\53.1.py", line 18, in <module>
main()
File "E:\文件\研究生\Python\程序\53.1.py", line 7, in main
response = urllib.request.urlopen(url)
File "D:\python\lib\urllib\request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "D:\python\lib\urllib\request.py", line 501, in open
req = Request(fullurl, data)
File "D:\python\lib\urllib\request.py", line 320, in __init__
self.full_url = url
File "D:\python\lib\urllib\request.py", line 346, in full_url
self._parse()
File "D:\python\lib\urllib\request.py", line 375, in _parse
raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: 'www.douban.com'
>>> 求助大佬能给个解释么?我原本自己写的代码也出现了这个问题,换成小甲鱼的也出现这个问题 试试
https://www.douban.com/ 一个是地址不对,需要加https://这样的前缀,一个是没加headers的话好像获取不了网页
import urllib.request
import chardet
def main():
url = input("请输入URL:")
url='https://'+url
request = urllib.request.Request(url=url)
request.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36')
response = urllib.request.urlopen(request)
html = response.read()
# 识别网页编码
encode = chardet.detect(html)['encoding']
if encode == 'GB2312':
encode = 'GBK'
print("该网页使用的编码是:%s" % encode)
if __name__ == "__main__":
main()
试试楼上的方法。这应该是你直接上域名,没有指定https之类的协议,导致的报错。
页:
[1]