Tôi đang cố gắng kiểm tra xem một từ nhất định có nằm trên một trang cho nhiều trang web hay không. Kịch bản chạy tốt để nói 15 trang web và sau đó nó dừng lại.codec utf8 không thể giải mã byte 0x96 trong python
UnicodeDecodeError: 'utf8' giải mã không thể giải mã byte 0x96 ở vị trí 15.344: start byte không hợp lệ
tôi đã tìm kiếm trên stackoverflow và tìm thấy nhiều vấn đề về nó nhưng tôi dường như không thể hiểu được những gì đã đi sai trong trường hợp của tôi.
Tôi muốn giải quyết hoặc nếu có lỗi bỏ qua trang web đó. Xin lời khuyên làm thế nào tôi có thể làm điều này như tôi mới và mã dưới đây chính nó đã đưa cho tôi một ngày để viết. Bằng cách này các trang web mà kịch bản dừng lại trên là 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"
Theo ý kiến của Mark Tôi đã thay đổi mã để thực hiện BeautifulSoup
htmlfile = urllib.urlopen("http://www.homestead.com")
page = BeautifulSoup((''.join(htmlfile)))
print page.prettify()
bây giờ tôi nhận được lỗi này
page = BeautifulSoup((''.join(htmlfile)))
TypeError: 'module' object is not callable
Tôi đang thử ví dụ bắt đầu nhanh của họ từ http://www.crummy.com/software/BeautifulSoup/documentation.html#Quick%20Start. Nếu tôi sao chép dán nó thì mã hoạt động tốt.
TÔI CUỐI CÙNG nó hoạt động. Cảm ơn mọi sự giúp đỡ của bạn. Đây là mã cuối cùng.
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"
Tôi thay vì bỏ qua trang web này, tôi có thể làm như giải mã ('utf8', somecodeforerrortoskip) –
user976847: Có rất nhiều lợi thế khác khi sử dụng BeautifulSoup. Tôi nghĩ bạn nên cho nó đi. –
Tôi có một cái nhìn tại nó nhờ –