亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

anaconda python2 編碼問題

class HtmlOutputer(object):
??? def __init__(self):
??????? self.datas=[]

??? def collect_data(self,data):
??????? if data is None:
??????????? return
??????? self.datas.append(data)
???????
??? def output_html(self):
??????? fout=open('output.html','w')
???????
??????? fout.write('<html>')
??????? fout.write('<body>')
??????? fout.write('<table>')
???????
??????? # ascii
??????? for data in self.datas:
??????????? fout.write('<tr>')
??????????? fout.write('<td>%s</td>' %data['url'].encode('utf-8'))
??????????? fout.write('<td>%s</td>' %data['title'].encode('utf-8'))
??????????? fout.write('<td>%s</td>' %data['summary'].encode('utf-8'))??????????
??????????? fout.write('</tr>')
???????
??????? fout.write('</table>')
??????? fout.write('</body>')
??????? fout.write('</html>')
??????? fout.close()

在爬蟲課程里,這個輸出器,無論我加了encode或者decode,都會出現ascii編碼錯誤,我用的是anaconda中的spyder python2.7 編譯的,也在網上找了很多方法,包括那個reload(sys) 都不行,你們有誰能解決這個問題嗎

正在回答

1 回答

#?Python?3.7?example
class?HtmlOutput(object):
????def?__init__(self):
????????self.store?=?[]
????def?save(self,?data):
????????if?data?is?None:
????????????return
????????#?data:?{url,?title,?summary}
????????self.store.append(data)
????def?output(self):
????????document?=?open('./output.html',?'w')
????????document.write('<html>')
????????document.write('<head><meta?charset=utf-8><title>Spider?Baike</title></head>')
????????document.write('<style>a{color:?#000;?text-decoration:?none}</style>')
????????document.write('<body>')
????????for?data?in?self.store:
????????????document.write('<h1><a?href="%s">%s</a></h1>'?%?(data['url'],?data['title']))
????????document.write('<p>%s</p>'?%?data['summary'])
????????document.write('</body>')
????????document.write('</html>')
????????document.close()
????????print?('Spider?is?over,?look?at?output.html')


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Python開發簡單爬蟲
  • 參與學習       227596    人
  • 解答問題       1288    個

本教程帶您解開python爬蟲這門神奇技術的面紗

進入課程

anaconda python2 編碼問題

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號