beautifulsoup的findAll find_all [英] beautifulsoup findAll find_all

查看:3266
本文介绍了beautifulsoup的findAll find_all的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析与Python中的HTML文件,我使用的模块是beautifulsoup。

我用它之后,一些奇怪happened.It据说功能find_all是

一样的findAll,但我已经尝试过他们两个。但它是不同的

谁能告诉我有什么不同?

 进口的urllib,urllib2的,cookielib
从BeautifulSoup进口*
网站=htt​​p://share.dmhy.org/topics/list?keyword=TARI+TARI+team_id%3A407RQSTR = urllib2.Request(网站)
RQ = urllib2.urlopen(RQSTR)
fchData = rq.read()汤= BeautifulSoup(fchData)T = soup.findAll('TR')
印花T


解决方案

在BeautifulSoup版本4中,方法是完全一样的;在混合大小写版本(的findAll findAllNext nextSibling 等)都被重命名为符合 Python的风格指南,但是的的名称仍然可以使移植更容易。请参见方法名称获得完整列表。

在新的code,你应该使用小写版本,因此 find_all

在你的榜样但是,你正在使用BeautifulSoup的第3版的(停产自2012年3月,的的使用它,如果你能帮助它),其中只有的findAll()可用。因为如果你正在寻找该名称的标签未知属性的名称(如 .find_all ,只在BeautifulSoup 4个可用)进行处理。没有< find_all方式>您的文档中标记,因此返回为

I would like to parse a html file with Python, and the module I used is beautifulsoup.

After I used it, something strange happened.It is said that the function "find_all" is

same as "findAll", but I've tried both of them. But it is different.

Can anyone tell me the 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')
print t

解决方案

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.

In new code, you should use the lowercase versions, so find_all, etc.

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.

这篇关于beautifulsoup的findAll find_all的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆