"findAll"与"findAll"之间的区别和"find_all"在BeautifulSoup中 [英] Difference between "findAll" and "find_all" in BeautifulSoup
问题描述
我想用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')
有人可以告诉我区别吗?
Can anyone tell me the difference?
推荐答案
在BeautifulSoup版本4中,方法完全相同.混合大小写的版本(findAll
,findAllNext
,nextSibling
等)都已重命名为符合方法名称.
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 版本3 (自2012年3月起停产,如果可以提供帮助,请不要使用它),其中只有.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"与"findAll"之间的区别和"find_all"在BeautifulSoup中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!