Python 如何使用请求库获取中文内容

Python 如何使用请求库获取中文内容,python,Python,我正在使用请求库(Python 2.7.10 64位\Visual Studio\Mac OS X)获取url: 但结果不是中国人: '<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible conten

我正在使用请求库(Python 2.7.10 64位\Visual Studio\Mac OS X)获取url:

但结果不是中国人:

'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>\xe6\x96\xb0\xe9\x97\xbb</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>\xe5\x9c\xb0\xe5\x9b\xbe</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>\xe8\xa7\x86\xe9\xa2\x91</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>\xe8\xb4\xb4\xe5\x90\xa7</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>\xe7\x99\xbb\xe5\xbd\x95</a> </noscript> <script>document.write(\'<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\'+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ \'" name="tj_login" class="lb">\xe7\x99\xbb\xe5\xbd\x95</a>\');\r\n                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">\xe6\x9b\xb4\xe5\xa4\x9a\xe4\xba\xa7\xe5\x93\x81</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>\xe5\x85\xb3\xe4\xba\x8e\xe7\x99\xbe\xe5\xba\xa6</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>\xe4\xbd\xbf\xe7\x94\xa8\xe7\x99\xbe\xe5\xba\xa6\xe5\x89\x8d\xe5\xbf\x85\xe8\xaf\xbb</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>\xe6\x84\x8f\xe8\xa7\x81\xe5\x8f\x8d\xe9\xa6\x88</a>&nbsp;\xe4\xba\xacICP\xe8\xaf\x81030173\xe5\x8f\xb7&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n'
'\r\n\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93文档。写入(\'\')\r\n

©;2017百度\xe4\xba\xacICP\xe8\xaf\x81030173\xe5\x8f\xb7

\r\n'

如何让内容成为中文?

属性返回字节数据,而不是文本。您可以通过解码将其转换为文本:

result = con.content.decode('utf-8')
这将返回文本

或者,您可以使用
文本
属性:

result = con.text
但是,百度没有发送正确的字符集标题,因此
con.text
将使用错误的编码并返回垃圾。您可以通过手动设置
encoding
属性来解决此问题,不过:

con.encoding = 'utf-8'
result = con.text

content
属性返回字节数据,而不是文本。您可以通过解码将其转换为文本:

result = con.content.decode('utf-8')
这将返回文本

或者,您可以使用
文本
属性:

result = con.text
但是,百度没有发送正确的字符集标题,因此
con.text
将使用错误的编码并返回垃圾。您可以通过手动设置
encoding
属性来解决此问题,不过:

con.encoding = 'utf-8'
result = con.text

相关:收到答案后,请不要更改您的问题。如果您还有其他问题,请发布新问题。相关:收到答案后,请不要更改您的问题。如果您还有其他问题,请发布一个新问题。它仍然会显示如下文本:“>\u4f7f\u7528\u767e\u5ea6\u524d\u5fc5\u8bfb”@Dolphin是的,这是正确的结果。尝试打印(结果),python将正确显示unicode字符。是的,打印结果包含chiese,但是如何使用此代码获得结果:if'作者' 在book_item.text中:@Dolphin我不确定我是否理解你的要求<代码>'作者' 在book_item.text中将返回False,因为“作者" 在该网站的任何地方都不会出现。我正在使用中文上下文来匹配结果作者' 我举一个例子来解释下一步操作的输出文本,它可以是任何中文上下文。现在打印输出是中文的,但结果变量不包含任何中文。它仍然是这样的文本:“>\u4f7f\u7528\u767e\u5ea6\u524d\u5fc5\u8bfb”@Dolphin是的,这是正确的结果。尝试
print(result)
和python将正确显示unicode字符。是的,打印结果包含chiese,但如何使用此代码获得结果:if'作者' 在book_item.text中:@Dolphin我不确定我是否理解您的要求。
'作者' 在book_item.text中
将返回False,因为“作者“在该网站的任何地方都没有出现。我使用中文上下文来匹配结果,”作者' 我举一个例子来解释下一步操作的输出文本,它可以是任何中文上下文。现在打印输出是中文的,但结果变量不包含任何中文。