相关疑难解决方法(0)

utf8编解码器无法解码python中的字节0x96

我正在尝试检查许多网站的页面上是否有某个单词.该脚本可以运行15个站点,然后停止.

UnicodeDecodeError:'utf8'编解码器无法解码位置15344中的字节0x96:无效的起始字节

我对stackoverflow进行了搜索,发现了很多问题,但我似乎无法理解我的情况出了什么问题.

我想要解决它,或者如果有错误跳过该网站.请告诉我如何做到这一点,因为我是新手,下面的代码本身花了我一天的时间来写.顺便说一下脚本停止的网站是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"
Run Code Online (Sandbox Code Playgroud)

根据Mark的评论,我改变了代码来实现beautifulsoup

htmlfile = urllib.urlopen("http://www.homestead.com")
page = BeautifulSoup((''.join(htmlfile)))
print page.prettify() 
Run Code Online (Sandbox Code Playgroud)

现在我收到了这个错误

page = BeautifulSoup((''.join(htmlfile)))
TypeError: 'module' object is not callable
Run Code Online (Sandbox Code Playgroud)

我正在尝试从http://www.crummy.com/software/BeautifulSoup/documentation.html#Quick%20Start快速启动示例.如果我复制粘贴它,那么代码工作正常.

我终于开始工作了.感谢大家的帮助.这是最终的代码.

import urllib
import re …
Run Code Online (Sandbox Code Playgroud)

python

24
推荐指数
2
解决办法
3万
查看次数

标签 统计

python ×1