代码如下:
# -*- coding: utf-8 -*-
#http://www.qiushibaike.com/8hr/page/1?s=4603425
import urllib2
from bs4 import BeautifulSoup
page=1
xiubai=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)就会报同样错误
慕工程0101907
繁华开满天机
呼啦一阵风