龙空技术网

Python判断网页编码

爱音乐的程序员小新人 303

前言:

今天小伙伴们对“python网址编码转换”大致比较关切,同学们都想要分析一些“python网址编码转换”的相关资讯。那么小编同时在网摘上网罗了一些关于“python网址编码转换””的相关资讯,希望看官们能喜欢,姐妹们快快来学习一下吧!

有一种渴,只有酒才能滋润,这种渴就是孤独。

根据网页返回编码寻找数据

比如我要找到这个网页的标题,那么直接正则匹配(.*?)就可以,但是许多时候因为编码问题requests这个库没办法正确解析,所以获取不到数据。

解决办法:

r_port_top = requests.get(url=str('http://'+url), headers=headers, timeout=5)

if r_port_top.encoding == 'ISO-8859-1':

encodings = requests.utils.get_encodings_from_content(r_port_top.text)

if encodings:

encoding = encodings[0]

else:

encoding = r_port_top.apparent_encoding

encode_content = r_port_top.content.decode(encoding, 'replace').encode('utf-8', 'replace')

port_title = re.search('<title>(.*?)</title>', encode_content, re.S).group().replace('<title>',

'').replace(

'</title>', '')

123456789101112

这种办法就是先判断网页的编码,然后转换之。但是有的时候是utf-8编码就没办法,接下来来个终极版的。

try:

UA = random.choice(headerss)

headers = {'User-Agent': UA}

r_port_top = requests.get(url=str('http://'+url), headers=headers, timeout=5)

if r_port_top.encoding == 'ISO-8859-1':

encodings = requests.utils.get_encodings_from_content(r_port_top.text)

if encodings:

encoding = encodings[0]

else:

encoding = r_port_top.apparent_encoding

encode_content = r_port_top.content.decode(encoding, 'replace').encode('utf-8', 'replace')

port_title = re.search('<title>(.*?)</title>', encode_content, re.S).group().replace('<title>',

'').replace(

'</title>', '')

elif r_port_top.encoding == 'GB2312':

encodings = requests.utils.get_encodings_from_content(r_port_top.text)

if encodings:

encoding = encodings[0]

else:

encoding = r_port_top.apparent_encoding

encode_content = r_port_top.content.decode(encoding, 'replace').encode('utf-8', 'replace')

port_title = re.search('<title>(.*?)</title>', encode_content, re.S).group().replace('<title>',

'').replace(

'</title>', '')

elif r_port_top.encoding == 'gb2312':

encodings = requests.utils.get_encodings_from_content(r_port_top.text)

if encodings:

encoding = encodings[0]

else:

encoding = r_port_top.apparent_encoding

encode_content = r_port_top.content.decode(encoding, 'replace').encode('utf-8', 'replace')

port_title = re.search('<title>(.*?)</title>', encode_content, re.S).group().replace('<title>',

'').replace(

'</title>', '')

elif r_port_top.encoding == 'GBK':

encodings = requests.utils.get_encodings_from_content(r_port_top.text)

if encodings:

encoding = encodings[0]

else:

encoding = r_port_top.apparent_encoding

encode_content = r_port_top.content.decode(encoding, 'replace').encode('utf-8', 'replace')

port_title = re.search('<title>(.*?)</title>', encode_content, re.S).group().replace('<title>',

'').replace(

'</title>', '')

elif r_port_top.encoding == 'gbk':

encodings = requests.utils.get_encodings_from_content(r_port_top.text)

if encodings:

encoding = encodings[0]

else:

encoding = r_port_top.apparent_encoding

encode_content = r_port_top.content.decode(encoding, 'replace').encode('utf-8', 'replace')

port_title = re.search('<title>(.*?)</title>', encode_content, re.S).group().replace('<title>',

'').replace(

'</title>', '')

else:

port_title = re.search('<title>(.*?)</title>', r_port_top.content, re.S).group().replace('<title>',

'').replace(

'</title>', '')

except:

try:

port_title = re.search('<title>(.*?)</title>', r_port_top.content, re.S).group().replace('<title>',

'').replace(

'</title>', '')

except:

port_title = '暂时无法获取网站标题'

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566使用chardet直接判断转换

上面那个方法实在是太傻了,使用chardet轻松解决网页编码问题。

# -*- coding: utf-8 -*-

# @Time : 2018/5/4 0004 8:55

# @Author : Langzi

# @Blog :

# @File : get urls.py

# @Software: PyCharm

import sys

import chardet

import re

import requests

reload(sys)

sys.setdefaultencoding('utf-8')

url = ''

d1 = requests.get(url)

print d1.content

if isinstance(d1.content,unicode):

pass

else:

codesty = chardet.detect(d1.content)

a = d1.content.decode(codesty['encoding'])

1234567891011121314151617181920212223

得到的a就是网页最终编码后的结果,这个时候直接re.search(‘(.*?)‘,a)就可以达到了匹配所有网址的标题了。

标签: #python网址编码转换