这是一个创建于 2512 天前的主题,其中的信息可能已经有所发展或是发生改变。
只是爬虫简单的日期和天气情况, 但是出现乱码情况,一直困扰我,没有得到解决。
#-*- coding:utf-8 -*-
import requests
from lxml import html
r = requests.get("http://www.weather.com.cn/weather/101070601.shtml")
rawdata = html.fromstring(r.text)
in_row = rawdata.xpath('//div[@id="7d"]/ul/li/h1/text() | //div[@id="7d"]/ul/li/p[1]/text()')
for i in in_row:
intro = i.encode('utf-8')
print(intro)
输出结果是这样的:
b'15\xc3\xa6\xc2\x97\xc2\xa5\xc3\xaf\xc2\xbc\xc2\x88\xc3\xa4\xc2\xbb\xc2\x8a\xc3\xa5\xc2\xa4\xc2\xa9\xc3\xaf\xc2\xbc\xc2\x89'
b'\xc3\xa6\xc2\x99\xc2\xb4'
后面还有很多 我就不复制了。
--------------------分割线------------------------------
对了 我直接 print(r.text) 输出的中文那些都是乱码情况 。
|
|
1
wq67200976 2017-11-15 19:49:08 +08:00
我之前也碰到过这种问题,不知道是不是 python2 和 3 的区别,2 的话就没问题,你把他输出到文件里打开也是正常
|
|
|
2
hcnhcn012 2017-11-15 21:07:17 +08:00 via iPhone
i.encode('utf-8')
|
|
|
3
Marsss 2017-11-16 09:01:37 +08:00
不要用 r.text,这样 rawdata = html.fromstring(r.content) ,然后下面就不需要 i.encode('utf-8')了,直接打印
|
|
|
4
lance418 2017-11-16 09:38:38 +08:00
@ Marsss 谢谢 正常输出了 感谢大佬指点 给大佬递茶 0.0
|