无法写中文到txt文档里去
#---------------------------------import---------------------------------------
import urllib2; import re;
#------------------------------------------
def main():
userMainUrl = "http://list.jd.com/1320-1585-1598-0-0-0-0-0-0-0-1-4-1-1.html";
req = urllib2.Request(userMainUrl);
resp = urllib2.urlopen(req);
respHtml = resp.read();
p1 = re.search(r'review/1001097102-1-1.html.?>(.*?)</a>', respHtml);
p2 = re.search(r'review/251308-1-1.html.?>(.*?)</a>', respHtml);
print p1.group(1)
print p2.group(1)
aaa=p1.group(1)
print aaa
file='e:\\test.txt'
a=u'aaa'
b=a.encode('gb2312')
f=open(file,"w")
f.write(a)
f.close()
print "successful"
###############################################################################
if __name__=="__main__":
main();
[解决办法]
如果你用了encode(),那变量就是字节型
打开文档模式要用'wb'