我正在尝试检查某个单词是否位于许多网站的页面上。该脚本运行良好,说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还有很多其他优势。我认为你应该放弃它。 –
我看看它谢谢 –