找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2527|回复: 3

[求助] python2.7 unicode 中文乱码

8

主题

20

帖子

20

积分

贫民

积分
20
ok0755 发表于 2018-1-9 16:28:17 | 显示全部楼层 |阅读模式
如题,代码贴在下面。返回天气,结果为乱码,百度了很久,尝试了N种方法,都不成功,请教,谢谢。
  1. #coding=utf-8
  2. import urllib2
  3. import requests
  4. import lxml
  5. from bs4 import BeautifulSoup

  6. url= "http://www.weather.com.cn/weather/101220304.shtml"  #构造网址 网站meta  utf-8编码
  7. req=requests.get(url).text
  8. soup=BeautifulSoup(req,'html.parser')
  9. for inpu in soup.findAll('ul',class_='t clearfix'):
  10.     for text in inpu.stripped_strings:
  11.         #print type(text)  这里显示type(text)为unicode
  12.         print text.decode('utf-8')
复制代码
返回结果:
9日(今天)
晴
7
/
-3℃
3-4级
10日(明天)
晴
6
/
-3℃
3-4级转<3级
11日(后天)
多云转晴
6
/
-5℃
<3级
12日(周五)
晴
5
/
-5℃
<3级
13日(周六)
多云
7
/
1℃
<3级
14日(周日)
多云
10
/
3℃
<3级
15日(周一)
多云转小雨
9
/
4℃
<3级

回复

使用道具 举报

8

主题

20

帖子

20

积分

贫民

积分
20
ok0755  楼主| 发表于 2018-1-9 17:03:24 | 显示全部楼层
?????????????
回复

使用道具 举报

8

主题

20

帖子

20

积分

贫民

积分
20
ok0755  楼主| 发表于 2018-1-10 09:49:09 | 显示全部楼层
回复

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
pyoge 发表于 2018-1-10 16:56:50 | 显示全部楼层
  1. #coding=utf-8
  2. import urllib2
  3. import requests
  4. import lxml
  5. from bs4 import BeautifulSoup

  6. url= "http://www.weather.com.cn/weather/101220304.shtml"  #构造网址 网站meta  utf-8编码
  7. req=requests.get(url).content#用content
  8. soup=BeautifulSoup(req,'html.parser')
  9. for inpu in soup.findAll('ul',class_='t clearfix'):
  10.     for text in inpu.stripped_strings:
  11.         #print type(text)  这里显示type(text)为unicode
  12.         print text
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表