“findAll"之间的区别和“find_all"在美丽汤

Difference between quot;findAllquot; and quot;find_allquot; in BeautifulSoup(“findAll之间的区别和“find_all在美丽汤)
本文介绍了“findAll"之间的区别和“find_all"在美丽汤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想用 Python 解析一个 HTML 文件,我使用的模块是 BeautifulSoup.

I would like to parse an HTML file with Python, and the module I am using is BeautifulSoup.

据说函数find_allfindAll是一样的.我都试过了,但我相信它们是不同的:

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 中,方法完全相同;混合大小写版本(findAllfindAllNextnextSibling 等)都已重命名以符合 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"在美丽汤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)