[英]How to retrieve the original string from UTF-8 file?
I am doing some web scraping with python and BeautifulSoup. 我正在用python和BeautifulSoup进行一些Web抓取。
body = soup.find("article")
tempvar = body.find()
fuu = open('tempfile', 'w')
tempvar = tempvar.encode('utf-8')
fuu.write(str(tempvar))
fuu.close()
fupa = open('tempfile')
joji = BeautifulSoup(fupa,'html.parser')
fupa.close()
print(joji)
tempvar would would contain html stuff , sometimes with emojis. tempvar将包含html内容,有时还会包含表情符号。 I want to use the contents of the file tempfile later in a real html file.
我想稍后在实际的html文件中使用文件tempfile的内容。
The print(joji) produces something like this: print(joji)产生如下内容:
<b>mencapai\xc2\xa0batas aksara 140</b>, tapi sudah tentu itu tidak termasuk semua <i>tweet </i>yang tak pernah dihantar kerana pengguna tidak boleh nak luahkan apa yang mereka mahukan. Selepas <b>mengaktifkan aksara 280</b> pada <b>sejumlah kecil akaun </b>yang bertuah, <b>Twitter </b>mengatakan <b>hanya 1%</b> sahaja <b>pengguna yang capai had aksara 280</b>. Tulis panjang\xc2\xb2 nak buat karangan ka. \xf0\x9f\x98\x9c<br/>\n<br/>\nIa juga jarang berlaku bagi pengguna untuk mencapai aksara 280, hanya <b>2%</b> dari <i>tweet </i><b>melebihi aksara 190</b>. <b>Had aksara tweet sebanyak 280 </b>juga <b>mendapat lebih <i>likes </i>dan <i>retweets </i></b>daripada had aksara <i>tweet </i>sebanyak 140. \xf0\x9f\x98\x8a<br/>\n<br/>
tempvar
is a Unicode string. tempvar
是Unicode字符串。 To write it correctly to a file: 要将其正确写入文件:
with open('tempfile', 'w', encoding='utf8') as fuu:
fuu.write(tempvar)
Read it back in with: 通过以下方式读回:
with open('tempfile', encoding='utf8') as fupa:
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.