python 【编码与状态】

 1 import requests
 2 
 3 import re
 4 
 5 def getHtml(url):
 6     headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chro\
 7                me/55.0.2883.87 Safari/537.36'}
 8     response =requests.get(url,headers=headers)
 9     #编码
10     charset = (requests.utils.get_encodings_from_content(response.text))
11 
12     response.encoding = charset
13     if response.status_code == 200:
14         
15         status = response.status_code
16         #格式format
17         return ("状态:{}\n编码:{}".format(str(status),str(charset)))
18     return "Error"
19     
20 
21 def main():
22 
23     url = input("请输入网址:")
24 
25     charset=getHtml(url)
26 
27     print(charset)
28 
29 
30 main()    
编码

 

posted @ 2018-06-25 17:03  Justice-V  阅读(92)  评论(0)    收藏  举报