返回信息流#coding:utf-8
import re
import requests
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
class grab(object):
def __init__(self):
print('Go...')
def getsource(self,url):
html=requests.get(url)
return(html.text)
def geteveryclass(self,source):
everyclass=re.findall('<li id="1890" test="0" deg="0" >(.*?)<div id="fivelesson"></div>',source,re.S)
return (everyclass)
def getinfo(self,eachclass):
eachclass=re.findall('"lessonimg" title="(.*?)" alt="',eachclass,re.S)
print (eachclass)
return eachclass
url='http://www.jikexueyuan.com/'
sk=grab()
sk1=sk.getsource(url)
sk2=sk.geteveryclass(sk1)
sk3=sk.getinfo(str(sk2))
f = open('info.txt','w')
for i in sk3:
f.write(i)
f.write("\n")
f.close()
一个爬虫,文件写入没有问题,可编码一直是
['CSS3 \\u5b9e\\u73b0 loading \\u52a8\\u753b\\u548c 3D \\u7ffb\\u8f6c\\u52a8\\u753b\\u7279\\u6548',
这种样子,不知道怎么转回汉字,求教[ema1][ema1][ema1]
这是一条镜像帖。来源:北邮人论坛 / python / #8348同步于 2015/8/26
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
[问题]python里汉字编码的问题 (新手学习中)
sunkai1995
2015/8/26镜像同步10 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
In [1]: print u'CSS3 \u5b9e\u73b0 loading \u52a8\u753b\u548c 3D \u7ffb\u8f6c\u52a8\u753b\u7279\u6548'
CSS3 实现 loading 动画和 3D 翻转动画特效
f.write(i) 改成f.write(i.encode('utf8'))试试?
我试一下
【 在 hansnow (小寒) 的大作中提到: 】
: In [1]: print u'CSS3 \u5b9e\u73b0 loading \u52a8\u753b\u548c 3D \u7ffb\u8f6c\u52a8\u753b\u7279\u6548'
: CSS3 实现 loading 动画和 3D 翻转动画特效
: f.write(i) 改成f.write(i.encode('utf8'))试试?
通过『我邮2.0』发布
这个可以 谢谢~
【 在 hansnow (小寒) 的大作中提到: 】
: In [1]: print u'CSS3 \u5b9e\u73b0 loading \u52a8\u753b\u548c 3D \u7ffb\u8f6c\u52a8\u753b\u7279\u6548'
: CSS3 实现 loading 动画和 3D 翻转动画特效
: f.write(i) 改成f.write(i.encode('utf8'))试试?
通过『我邮2.0』发布
嗯嗯 我看下
【 在 soeaver (Shin-Hye) 的大作中提到: 】
: 试试这个 str(x).decode('unicode_escape').decode('unicode_escape')
通过『我邮2.0』发布
#coding:utf-8
import re
import requests
import codecs
class grab(object):
def __init__(self):
print('Go...')
def getsource(self,url):
html=requests.get(url)
return(html.text)
def geteveryclass(self,source):
everyclass=re.findall('<li id="1890" test="0" deg="0" >(.*?)<div id="fivelesson"></div>',source,re.S)
return (everyclass)
def getinfo(self,eachclass):
eachclass=re.findall('"lessonimg" title="(.*?)" alt="',eachclass,re.S)
print (eachclass)
return eachclass
url='http://www.jikexueyuan.com/'
sk=grab()
sk1=sk.getsource(url)
sk2=sk.geteveryclass(sk1)
sk3=sk.getinfo(" ".join(sk2))
f = codecs.open('info.txt','w', "utf-8")
for i in sk3:
f.write("%s\n" % i)
f.close()
试试这个,程序运行阶段尽量一直是unicode编码,输入输出时进行编码转换
我貌似执行错程序了 还是不行 我把爬出来的列表转换成str 然后在主程序里再转换回列表输出就是文字了
【 在 napoleonwxu (得之,我幸;不得,我命。) 的大作中提到: 】
: 可以么?为什么我试了还是不行呢
通过『我邮2.0』发布
嗯啊 这个方法没问题~感谢
【 在 anttylove (antty) 的大作中提到: 】
: #coding:utf-8
: import re
: import requests
: ...................
通过『我邮2.0』发布