Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用python对网页进行解码和编码?_Python_Web_Encoding - Fatal编程技术网

如何用python对网页进行解码和编码?

如何用python对网页进行解码和编码?,python,web,encoding,Python,Web,Encoding,我使用Beautifulsoup和urllib2下载网页,但不同的网页有不同的编码方法,如utf-8、gb2312、gbk。我使用urllib2获取搜狐的主页,该主页使用gbk编码,但在我的代码中,我也使用这种方式解码其网页: self.html_doc = self.html_doc.decode('gb2312','ignore') 但是,在使用BeautifulSoup将页面解码为unicode之前,我如何知道页面使用的编码方法呢?在大多数中文网站中,http头字段中没有内容类型 使用B

我使用Beautifulsoup和urllib2下载网页,但不同的网页有不同的编码方法,如utf-8、gb2312、gbk。我使用urllib2获取搜狐的主页,该主页使用gbk编码,但在我的代码中,我也使用这种方式解码其网页:

self.html_doc = self.html_doc.decode('gb2312','ignore')

但是,在使用BeautifulSoup将页面解码为unicode之前,我如何知道页面使用的编码方法呢?在大多数中文网站中,http头字段中没有内容类型

使用BeautifulSoup,您可以解析HTML并访问属性:

import urllib2
from bs4 import BeautifulSoup

html = urllib2.urlopen('http://www.sohu.com').read()
soup = BeautifulSoup(html)

>>> soup.original_encoding
u'gbk'
这与HTML中标记中声明的编码一致:

但由于HTML已经作为unicode提供,因此没有太多意义:

>>> soup.a['title']
u'\u641c\u72d0-\u4e2d\u56fd\u6700\u5927\u7684\u95e8\u6237\u7f51\u7ad9'
>>> print soup.a['title']
搜狐-中国最大的门户网站
>>> soup.a.text
u'\u641c\u72d0'
>>> print soup.a.text
搜狐
也可以尝试使用chardet模块检测它,尽管它有点慢:

>>> import chardet
>>> chardet.detect(html)
{'confidence': 0.99, 'encoding': 'GB2312'}

我知道这是一个老问题,但我今天花了一段时间对一个特别有问题的网站感到困惑,所以我想我应该分享一个对我有效的解决方案,我从这里得到了:

请求有一个功能,可以自动获取网站的实际编码,这意味着在我发现这个之前,你不必费劲地对它进行编码/解码,我在尝试对字符串/字节进行编码/解码时遇到了各种各样的错误,而且从来没有得到任何可读的输出。这种特性称为表观编码。以下是它对我的作用:

from bs4 import BeautifulSoup
import requests

url = 'http://url_youre_using_here.html'
readOut = requests.get(url)
readOut.encoding = readOut.apparent_encoding #sets the encoding properly before you hand it off to BeautifulSoup
soup = BeautifulSoup(readOut.text, "lxml")
另一个解决方案

from simplified_scrapy.request import req
from simplified_scrapy.simplified_doc import SimplifiedDoc
html = req.get('http://www.sohu.com') # This will automatically help you find the correct encoding
doc = SimplifiedDoc(html)
print (doc.title.text)

非常感谢你!我是初学者。嗯,我也有一个问题。也许,这对你来说很容易。我尝试下载的html网页,解压后,它有一个UnicodeDecodeError:“ascii”编解码器无法解码第1位的字节0x8b:序号不在范围128中。所以我尝试将它从gb2312解码为unicode,然后使用beautifulsoup,如果我没有,它就有unicodeDecodeError。@张永胜:该页面存在编码问题-它声称标记中是gb2312,chardet同意,但是,至少有一个非gb2312字节序列字节143328-143329=\x87N==嘚. 您可以使用GBK对该页面进行解码GB2312的超集,即soup=beautifulsoupurlib2.urlopen'http://www.sina.com.cn/“,来自_encoding='gbk'。您可以使用html=urllib2.urlopen'http://www.sina.com.cn/阅读,解码“gbk”,你真是个专家!非常感谢
from bs4 import BeautifulSoup
import requests

url = 'http://url_youre_using_here.html'
readOut = requests.get(url)
readOut.encoding = readOut.apparent_encoding #sets the encoding properly before you hand it off to BeautifulSoup
soup = BeautifulSoup(readOut.text, "lxml")
from simplified_scrapy.request import req
from simplified_scrapy.simplified_doc import SimplifiedDoc
html = req.get('http://www.sohu.com') # This will automatically help you find the correct encoding
doc = SimplifiedDoc(html)
print (doc.title.text)