問題描述
我想用 Python 解析一個 HTML 文件,我使用的模塊是 BeautifulSoup.
I would like to parse an HTML file with Python, and the module I am using is BeautifulSoup.
據說函數find_all
和findAll
是一樣的.我都試過了,但我相信它們是不同的:
It is said that the function find_all
is the same as findAll
. I've tried both of them, but I believe they are different:
import urllib, urllib2, cookielib
from BeautifulSoup import *
site = "http://share.dmhy.org/topics/list?keyword=TARI+TARI+team_id%3A407"
rqstr = urllib2.Request(site)
rq = urllib2.urlopen(rqstr)
fchData = rq.read()
soup = BeautifulSoup(fchData)
t = soup.findAll('tr')
誰能告訴我區別?
推薦答案
BeautifulSoup 4 中,方法完全相同;混合大小寫版本(findAll
、findAllNext
、nextSibling
等)都已重命名以符合 Python 風格指南,但 舊 名稱仍然可以使移植更容易.完整列表請參見方法名稱.
In BeautifulSoup version 4, the methods are exactly the same; the mixed-case versions (findAll
, findAllNext
, nextSibling
, etc.) have all been renamed to conform to the Python style guide, but the old names are still available to make porting easier. See Method Names for a full list.
在新代碼中,你應該使用小寫版本,所以find_all
等
In new code, you should use the lowercase versions, so find_all
, etc.
然而,在您的示例中,您使用的是 BeautifulSoup version 3(自 2012 年 3 月起停止使用,如果您能提供幫助,請不要使用它),其中只有 findAll()
可用.未知屬性名稱(例如 .find_all
,僅在 BeautifulSoup 4 中可用)被視為您正在搜索該名稱的標簽.您的文檔中沒有 <find_all>
標記,因此為此返回 None
.
In your example however, you are using BeautifulSoup version 3 (discontinued since March 2012, don't use it if you can help it), where only findAll()
is available. Unknown attribute names (such as .find_all
, which only is available in BeautifulSoup 4) are treated as if you are searching for a tag by that name. There is no <find_all>
tag in your document, so None
is returned for that.
這篇關于“findAll"之間的區別和“find_all"在美麗湯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!