0
我試圖從谷歌的HTML代碼字符串寫入文件在Python 3.4Python中的一些問題解碼用繩子
#coding=utf-8
try:
from urllib.request import Request, urlopen # Python 3
except:
from urllib2 import Request, urlopen # Python 2
useragent = 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'
#Generate URL
url = 'https://www.google.com.tw/search?q='
query = str(input('Google It! :'))
full_url = url+query
#Request Data
data = Request(full_url)
data.add_header('User-Agent', useragent)
dataRequested = urlopen(data).read()
dataRequested = str(dataRequested.decode('utf-8'))
print(dataRequested)
#Write Data Into File
file = open('Google - '+query+'.html', 'w')
file.write(dataRequested)
它可以正確打印字符串, 但是當它寫入文件, 它會顯示
file.write(dataRequested)
UnicodeEncodeError: 'cp950' codec can't encode character '\u200e' in position 97658: illegal multibyte sequence
我試圖改變解碼方式,但它不起作用。 我試圖替換\ u200e,但它會帶來更多的編碼字符錯誤。
加油!我只是拼寫了幾個字。 –
啊哈,我現在明白了。謝謝。對於我的生活,我看不出這就是你的意思! 「get string」和「do not get」的並置是令人困惑的。抱歉。會讀得更好*雖然有些人不明白。感謝您的修復。 –
非常感謝。現在它工作得很好。 – BobbyHo