2016-12-03 44 views
0

大家好,新Python用戶在這裏我在構建一個非常基本的頁面刮板時遇到了一個奇怪的錯誤。頁面刮板解析器錯誤?

我使用BeautifulSoup4來幫助我,當我執行我的代碼,我得到這個錯誤

「UserWarning:沒有解析器明確規定,所以我用最好的HTML解析器該系統(」 HTML .parser「)。這通常不是問題,但是如果您在另一個系統或不同虛擬環境中運行此代碼,它可能會使用不同的分析器並且行爲不同。位於文件C:/ Users/***/PycharmProjects/untitled1/s.py的第13行。要刪除此警告,請更改如下所示的代碼:「

BeautifulSoup([your markup]) 

to this: 

    BeautifulSoup([your markup], "html.parser") 

    markup_type=markup_type)) 

如果有人有任何幫助解決這個問題,我將不勝感激!

代碼遵循

import requests 
from bs4 import BeautifulSoup 

def trade_spider(): 
    url = 'http://buckysroom.org/trade/search.php?page=' # Could add a + pls str(pagesomething) to add on to the url so that it would update 
    source_code = requests.get(url) #requests the data from the site 
    plain_text = source_code.text #imports all of the data gathered 
    soup = BeautifulSoup(plain_text) #This hold all of the data, and allows you to sort through all of the data, converts it 
    for link in soup.find_all('a', {'class' : 'item-name'}): 
     href = link.get('href') 
     print(href) 

trade_spider() 
+1

的可能的複製[如何擺脫BeautifulSoup用戶警告?](http://stackoverflow.com/questions/33511544/how-to-get-rid-of-beautifulsoup-user-warning) –

回答

0

你可以嘗試以下行更改爲:

soup = BeautifulSoup(plain_text, "html.parser") 

或任何其他parser你需要使用...

+0

完美的工作!你能解釋一下爲什麼它有效嗎? –

+0

@NoahLinton,據我所知,BeautifulSoup需要指定的解析器能夠更好地識別您提供的Web內容並生成正確的解析結果。如果你在我的文章中查看鏈接,你會發現其他解析器也適用於不同類型的內容。欲瞭解更多信息:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#specifying-the-parser-to-use – coder