鱼C论坛

 找回密码
 立即注册
查看: 746|回复: 4

[已解决]写一个程序,检测指定 URL 的编码

[复制链接]
发表于 2019-7-12 22:03:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码不知道哪里出错了,运行没有报错,但是跑起来总是将指定网址直接打开了,而不返回我需要的编码类型
代码如下:


1   import urllib.request
2   import chardet


5   def main():
6       url = input('请输入网址:')
7       response = urllib.request.urlopen(url)
8       html = response.read()

10     encode = chardet.detect(html)['encoding']
11     if encode == 'GB2312':
12           encode = 'GBK'

14      print('该网页的编码是: %s' % encode)


15  if __name__ == '__main__':
16        main()


返回:
C:\Users\11749\Documents\python\venv\Scripts\python.exe C:/Users/11749/Documents/python/Py_110.py
请输入网址:https://www.liaoxuefeng.com/wiki ... 00/1183255880134144
Process finished with exit code -1

(自动打开网页之后迟迟没有后续反应,所以我自己手动结束了程序)

最佳答案
2019-7-13 18:05:20
两种方法:
import chardet, requests, urllib
from urllib.request import urlopen
url = 'https://www.liaoxuefeng.com/wiki ... 00/1183255880134144'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
                         '(KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 '
                         'Core/1.63.6823.400 QQBrowser/10.3.3117.400'}
res = requests.get(url, headers = headers)
s = res.encoding
print(s)


resp = urlopen('http://www.baidu.com')
html = resp.read()
chardet1 = chardet.detect(html)
m = chardet1['encoding']

print(m)
结果:
UTF-8
utf-8

Process finished with exit code 0

第二种方法用你原来的网址,报错503
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-13 06:50:21 | 显示全部楼层
你换个网址,这个代码没问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-13 10:49:33 | 显示全部楼层
新手·ing 发表于 2019-7-13 06:50
你换个网址,这个代码没问题

我这个代码里边有关于打开网页的命令吗
我试了几个都没有输出,但是网页到是一个不少都给我打开了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 17:34:42 | 显示全部楼层
。。我的意思是你输入别的网址
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-13 18:05:20 | 显示全部楼层    本楼为最佳答案   
两种方法:
import chardet, requests, urllib
from urllib.request import urlopen
url = 'https://www.liaoxuefeng.com/wiki ... 00/1183255880134144'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
                         '(KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 '
                         'Core/1.63.6823.400 QQBrowser/10.3.3117.400'}
res = requests.get(url, headers = headers)
s = res.encoding
print(s)


resp = urlopen('http://www.baidu.com')
html = resp.read()
chardet1 = chardet.detect(html)
m = chardet1['encoding']

print(m)
结果:
UTF-8
utf-8

Process finished with exit code 0

第二种方法用你原来的网址,报错503
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 01:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表