python27 unicode 中文乱码

ok075588 2018-01-10 03:56:07
如题,代码贴在下面。返回天气,结果为乱码,百度了很久,尝试了N种方法,都不成功,请教如何返回中文?谢谢。
#coding=utf-8
import urllib2
import requests
import lxml
from bs4 import BeautifulSoup

url= "http://www.weather.com.cn/weather/101220304.shtml" #构造网址 网站meta utf-8编码
req=requests.get(url).text
soup=BeautifulSoup(req,'html.parser')
for inpu in soup.findAll('ul',class_='t clearfix'):
for text in inpu.stripped_strings:
#print type(text) 这里显示type(text)为unicode
print text.decode('utf-8')

返回结果 为乱码
...全文
760 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ok075588 2018-01-16
  • 打赏
  • 举报
回复
谢谢热心解答
sanGuo_uu 2018-01-11
  • 打赏
  • 举报
回复
我是用python3写的 还有就是,BeautifulSoup不会用
#!/usr/bin/python
# -*- coding:utf-8 -*-

from urllib import request
import re
 
url= "http://www.weather.com.cn/weather/101220304.shtml"
req = request.Request(url)
html = request.urlopen(req).read().decode('utf-8')
#print(html)
patt=re.compile(r'<ul class="t clearfix">(.*?)</ul>',re.S)
res=patt.findall(html)
print(res)

with open('zz.txt','w') as f:
	f.write(res[0])
TheLittlePython 2018-01-11
  • 打赏
  • 举报
回复
response = requests.get(url) response.encoding = 'utf-8' req = response.text
坚强的豁着 2018-01-11
  • 打赏
  • 举报
回复
#coding=utf-8
import urllib2
import requests
import lxml
from bs4 import BeautifulSoup

url= "http://www.weather.com.cn/weather/101220304.shtml"  #构造网址 网站meta  utf-8编码
req=requests.get(url)
req.encoding = 'utf-8'
soup=BeautifulSoup(req.text,'html.parser')
for inpu in soup.findAll('ul',class_='t clearfix'):
    for text in inpu.stripped_strings:
        #print type(text)  #这里显示type(text)为unicode
        print text
oyljerry 2018-01-10
  • 打赏
  • 举报
回复

response = requests.get(url)
response.encoding = 'utf-8'

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧