首页 > Python BeautifulSoup爬网页中文输出编码问题

Python BeautifulSoup爬网页中文输出编码问题

环境:OS X 10.11
Python版本:2.7.10
IDE:PyCharm CE

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
# import urllib2
import requests

url = 'http://www.tripadvisor.cn/Attractions-g60763-Activities-New_York_City_New_York.html'
web_data = requests.get(url)
soup = BeautifulSoup(web_data.text, 'lxml')
titles = soup.select(
    'div.property_title > a[target="_blank"]')

for title in titles:
    data = {
        'title':title.get_text()
    }
    print data

结果为:

{'title': u'\u4e2d\u592e\u516c\u56ed'}
{'title': u'\u5927\u90fd\u4f1a\u827a\u672f\u535a\u7269\u9986'}
{'title': u'\u5ced\u77f3\u4e4b\u5dc5\u89c2\u666f\u53f0'}
{'title': u'9/11\u7eaa\u5ff5\u9986'}
{'title': u'\u66fc\u54c8\u987f\u5929\u9645\u7ebf'}
{'title': u'\u767e\u8001\u6c47'}
{'title': u'\u9ad8\u7ebf\u516c\u56ed'}
{'title': u'\u5927\u4e2d\u592e\u8f66\u7ad9'}
{'title': u'\u5f17\u91cc\u514b\u7f8e\u672f\u6536\u85cf\u9986'}
{'title': u'\u5e1d\u56fd\u5927\u53a6'}
{'title': u'\u5e03\u83b1\u6069\u516c\u56ed'}
{'title': u'\u4e16\u8d38\u5927\u53a6\u9057\u5740\u535a\u7269\u9986\u5de5\u4f5c\u5ba4'}

以上网站的编码采用了utf-8
请问为何爬出来的中文都是unicode字符,而不是中文。PyCharm中也都设置为了UTF-8。
是需要做什么转换吗?
谢谢


想问您解决了吗,我也碰到这个问题。


不需要,这已经可以直接用了


你把最后一句print data改成print title.get_text()看看


楼上还是没有解决打印时的编码问题,因为print字典等带有汉字的值的时候,编码会通过系统自动检测。
完美的解决办法是

print repr(a).decode("unicode–escape")
【热门文章】
【热门文章】