马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我是直接复制课后习题答案源代码的: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://这样的前缀,一个是没加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()
|