2017-06-27 36 views
-2

無論使用什麼html文件,我的輸出都會重複b''。BeautifulSoup輸出始終爲「b」'「

from bs4 import BeautifulSoup 
html_doc = "C:/Users/George/Desktop/bsTest" 
soup = BeautifulSoup(html_doc, 'html.parser') 

print(soup.prettify()) 

有人可以幫我解決這個問題嗎?

+0

嘗試添加.html到bsTest的末尾 – Splinxyy

+0

感謝您的快速響應,Splinxyy。可悲的是,這並沒有改變產量。 –

+1

是什麼讓你認爲你可以傳遞一個文件路徑到'BeautifulSoup'構造函數中?我建議在嘗試使用它之前閱讀模塊的文檔。 –

回答

1

您不能僅按照您嘗試傳遞文件路徑。請參閱文檔here

with open("C:/Users/George/Desktop/bsTest") as fp: soup = BeautifulSoup(fp, 'html.parser')

+0

謝謝你的建議。當我運行編輯的代碼時,我的輸出如下:<_io.TextIOWrapper name ='C:/Users/George/Desktop/HTMLs/temp_10_40.html'mode ='r'encoding ='cp1252'> –

+0

似乎正確。現在您需要按照文檔進行實際解析。 – Mrl0330

0

嘗試打開文檔您嘗試解析變量之前。

from bs4 import BeautifulSoup 
html_doc = "C:/Users/George/Desktop/bsTest" 
soup = BeautifulSoup(open(html_doc).read(), 'html.parser') 

print(soup.prettify()) 
+0

謝謝你的建議。當我運行編輯的代碼時,我的輸出如下:<_io.TextIOWrapper name ='C:/Users/George/Desktop/HTMLs/temp_10_40.html'mode ='r'encoding ='cp1252'> –

+0

我忘了將read()方法添加到打開。我更新了答案。試一試,讓我知道它是否有效。 – Dom

+0

使用更新後的代碼時,輸​​出保持不變。 –