2014-10-10 103 views
0

這裏是我的代碼,使用find_all使用 'find_all',但它與.find()的偉大工程:'NoneType' 對象不是可調用的BeautifulSoup

import requests 
from BeautifulSoup import BeautifulSoup 

r = requests.get(URL_DEFINED) 
print r.status_code 

soup = BeautifulSoup(r.text) 
print soup.find_all('ul') 

這是我得到:

Traceback (most recent call last): 


File "scraper.py", line 19, in <module> 
    print soup.find_all('ul') 
TypeError: 'NoneType' object is not callable 
+1

我認爲*你的問題是'find_all'只存在於BS4中(它將標準化命名爲更多PEP8的推薦),看起來你使用的是版本3,命名約定爲'.findAll' ...注意,使用'湯'('ul')'相當於*查找所有*在這兩個版本 – 2014-10-10 05:54:16

+0

我是新來的蟒蛇,我怎麼可以安裝BSS的BeautifulSoup – 2014-10-10 05:57:59

+0

我已經作出了答案 - 可能是在未來對其他人有用。我看到你熟悉點(從你以前的問題),所以應該對你有意義:) – 2014-10-10 06:06:09

回答

6

它看起來像你使用BeautifulSoup第3版,它使用一個稍微不同的命名約定,如:.findAll,而BeautifulSoup 4標準化命名更像是PEP8,例如:.find_all(但爲了向後兼容,保留了舊命名)。請注意0​​相當於兩者都找到全部

要下載並安裝,請使用pip install beautifulsoup4

然後改變你的進口是:

from bs4 import BeautifulSoup 

然後你就可以走了。

1

從這裏下載BS4。 http://www.crummy.com/software/BeautifulSoup/#Download

安裝它,並在你的代碼的開頭這樣導入:

import requests 
from bs4 import BeautifulSoup 

r = requests.get(URL_DEFINED) 
print r.status_code 

soup = BeautifulSoup(r.text) 
print soup.find_all('ul') 
相關問題