编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Python判断网页编码(python如何判断网页加载完成)

wxchong 2024-07-18 09:57:53 开源技术 11 ℃ 0 评论

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

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

比如我要找到这个网页的标题,那么直接正则匹配(.*?)就可以,但是许多时候因为编码问题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>', '')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

这种办法就是先判断网页的编码,然后转换之。但是有的时候是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 = '暂时无法获取网站标题'

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

使用chardet直接判断转换

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

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

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

# @Author : Langzi

# @Blog : www.langzi.fun

# @File : get urls.py

# @Software: PyCharm

import sys

import chardet

import re

import requests

reload(sys)

sys.setdefaultencoding('utf-8')

url = 'https://stackoverflow.com'

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'])

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表