将阿拉伯文本保存到文本文件中时,结果是一个奇怪的符号
import mechanicalsoup as ms
Browser = ms.StatefulBrowser()
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
url_status = "https://mobile.twitter.com/Orahbeeni/status/1063386449054175232"
Browser.open(url_status)
Browser.follow_link("/Orahbeeni/status/1063386449054175232")
html = Browser.get_current_page()
html = html.decode('utf8')
file_key = open("twtet.txt", 'w+')
file_key.write(str(html))
file_key.close()
文件twtet.txt:
答案 0 :(得分:0)
没有积分设置sys.defaultencoding
。只需将“ UTF-8”数据直接写入磁盘即可。
import mechanicalsoup as ms
Browser = ms.StatefulBrowser()
url_status = "https://mobile.twitter.com/Orahbeeni/status/1063386449054175232"
Browser.open(url_status)
Browser.follow_link("/Orahbeeni/status/1063386449054175232")
html = Browser.get_current_page()
with open("twtet.txt", 'wb+') as file_key:
file_key.write(html)
如果使用Python代码或其他应用程序打开文件,请确保以UTF-8文件打开。