我正在嘗試檢查某個單詞是否位於許多網站的頁面上。該腳本運行良好,說15個網站,然後停止。utf8編解碼器無法解碼python中的字節0x96
的UnicodeDecodeError:「UTF-8」編解碼器不能解碼位置15344字節0x96:無效的起始字節
我做了一個計算器搜索和發現了很多問題,但我似乎無法理解在我的情況下出了問題。
我想解決它,或者如果跳過該網站有錯誤。請教我如何做到這一點,因爲我是新手,下面的代碼本身讓我花了一天的時間寫作。順便說該腳本上暫停該網站是http://www.homestead.com
filetocheck = open("bloglistforcommenting","r")
resultfile = open("finalfile","w")
for countofsites in filetocheck.readlines():
sitename = countofsites.strip()
htmlfile = urllib.urlopen(sitename)
page = htmlfile.read().decode('utf8')
match = re.search("Enter your name", page)
if match:
print "match found : " + sitename
resultfile.write(sitename+"\n")
else:
print "sorry did not find the pattern " +sitename
print "Finished Operations"
按照馬克的意見,我改變了代碼來實現beautifulsoup
htmlfile = urllib.urlopen("http://www.homestead.com")
page = BeautifulSoup((''.join(htmlfile)))
print page.prettify()
現在我收到此錯誤
page = BeautifulSoup((''.join(htmlfile)))
TypeError: 'module' object is not callable
我正在嘗試從http://www.crummy.com/software/BeautifulSoup/documentation.html#Quick%20Start開始的快速入門示例。如果我複製粘貼它然後代碼工作正常。
我最終得到它的工作。感謝大家的幫助。這是最終的代碼。
import urllib
import re
from BeautifulSoup import BeautifulSoup
filetocheck = open("listfile","r")
resultfile = open("finalfile","w")
error ="for errors"
for countofsites in filetocheck.readlines():
sitename = countofsites.strip()
htmlfile = urllib.urlopen(sitename)
page = BeautifulSoup((''.join(htmlfile)))
pagetwo =str(page)
match = re.search("Enter YourName", pagetwo)
if match:
print "match found : " + sitename
resultfile.write(sitename+"\n")
else:
print "sorry did not find the pattern " +sitename
print "Finished Operations"
我寧願跳過這個網站,我可以像解碼一樣做('utf8',somecodeforerrortoskip) –
user976847:使用BeautifulSoup還有很多其他優勢。我認爲你應該放棄它。 –
我看看它謝謝 –