代码如下:# -*- coding: utf-8 -*-#http://www.qiushibaike.com/8hr/page/1?s=4603425import urllib2from bs4 import BeautifulSouppage=1xiubai=open(r'xiubai.txt','w+')for page in range(1,11): url="http://www.qiushibaike.com/8hr/page/"+str(page)+"?s=4603425" soup = BeautifulSoup(urllib2.urlopen(url).read()) for result in soup.findAll("div", "content", title=True): xiubai.write(result.text)报错:Traceback (most recent call last):
File "D:\python\python2.7.5\pythontest\爬虫\kanxiushibaike.py", line 13, in <module>
xiubai.write(result.text)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-10: ordinal not in range(128)不用result.text 直接result虽然包含了html的部分代码但是又可以转为str。之后就可以随便用了。但是直接str(result.text)就会报同样错误
- 2 回答
- 0 关注
- 375 浏览
添加回答
举报
0/150
提交
取消