2013-10-17 41 views
-2

我想了解BeautifulSoup的工作原理。請注意,我對Python非常陌生,所以我可能錯過了一些東西。BeautifulSoup - 爲什麼打印文件路徑而不是內容

我打開一個Python終端和這樣寫:

from bs4 import BeautifulSoup 
import re 
ytchannel = '/home/XXX/Documents/test2' 
soup = BeautifulSoup(ytchannel) 
print(soup.prettify()) 

這就是我得到:

<html> 
<body> 
    <p> 
    /home/XXX/Documents/test2 
    </p> 
</body> 
</html> 

爲什麼?對我來說完全是無稽之談。我只想要test2的內容。 我正在寫BeautifulSoup網站上寫的內容。

回答

1

您將一個字符串傳遞給BeautifulSoup();確定它是一個文件名,但BeautifulSoup()不會爲您打開文件名。它僅對字符串或打開的文件對象進行操作。

先打開文件;

with open(ytchannel) as infile: 
    soup = BeautifulSoup(infile) 

Making the soup:如果你把它們放到BeautifulSoup()將讀取的文件對象。

+0

謝謝你的幫助! – user1983400

相關問題