課程
/后端開發(fā)
/Python
/Python開發(fā)簡(jiǎn)單爬蟲
爬出來(lái)的全是b'\nPython\xef\xbc\x88\xe8\x8b\xb1\xe8\xaf\xad\xe5\x8f\x91\xe9\x9f\xb3亂碼,什么情況
2016-01-15
源自:Python開發(fā)簡(jiǎn)單爬蟲 7-7
正在回答
沒錯(cuò),編碼,又解碼,相當(dāng)于不起作用,但是起作用的是這一句“fout = open('output.html','w',encoding='utf-8')”,我也很奇怪,給文本編一次UTF編碼就好,MOOC的方式就出現(xiàn)了亂碼錯(cuò)誤,但是在文件的打開的時(shí)間進(jìn)行UTF編碼就可以避免亂碼了
open
(
'output.html'
,
'w'
,encoding=
'utf-8'
)
又encode,又decode,豈不是編碼沒有改???
在文件開頭加
#?-*-?coding:?utf-8?-*- import?sys reload(sys) sys.setdefaultencoding("utf-8")
??我的運(yùn)行環(huán)境是win7,32位,python3.5.1, ??修改解析器代碼 ??把#號(hào)后面的語(yǔ)句修改成下行,就好了! ????def?output_html(self): ????????#fout?=?open('output.html','w') ????????fout?=?open('output.html','w',encoding='utf-8') ???????? ????????#fout.write("<html><meta?charset=\"utf-8\"?/>") ????????fout.write("<head><meta?charset=\"utf-8\"></head>") ????????fout.write("<body>") ????????fout.write("<table>") ???????? ????????for?data?in?self.datas: ????????????fout.write("<tr>") ????????????fout.write("<td>%s</td>"?%?data['url']) ????????????#fout.write("<td>%s</td>"?%?data['title'].encode('utf-8')) ????????????fout.write("<td>%s</td>"?%?data['title'].encode('utf-8').decode("utf-8")) ????????????#fout.write("<td>%s</td>"?%?data['summary'].encode('utf-8')) ????????????fout.write("<td>%s</td>"?%?data['summary'].encode('utf-8').decode("utf-8"))
舉報(bào)
本教程帶您解開python爬蟲這門神奇技術(shù)的面紗
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2018-07-20
沒錯(cuò),編碼,又解碼,相當(dāng)于不起作用,但是起作用的是這一句“fout =
open
(
'output.html'
,
'w'
,encoding=
'utf-8'
)
”,我也很奇怪,給文本編一次UTF編碼就好,MOOC的方式就出現(xiàn)了亂碼錯(cuò)誤,但是在文件的打開的時(shí)間進(jìn)行UTF編碼就可以避免亂碼了2016-07-21
又encode,又decode,豈不是編碼沒有改???
2016-04-24
在文件開頭加
2016-01-16