有人能解释一下findAll
在BeautifulSoup中是如何工作的吗?在
我怀疑这一行:A = soup.findAll('strong',{'class':'name fn'})
。它看起来像是找到一些符合特定条件的字符。在
但是网页的原始代码是……<STRONG class="name fn">iPod nano 16GB</STRONG>
。。。。。。在
这个('strong',{'class':'name fn'})
是如何提取的?谢谢。在
原始Python代码
from bs4 import BeautifulSoup
import urllib2
import re
url="http://m.harveynorman.com.au/ipods-audio-music/ipods/ipods"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
A = soup.findAll('strong',{'class':'name fn'})
for B in A:
print B.renderContents()
来自文档:Beautifulsoup Docs
BeautifulSoup提供了许多遍历(遍历)解析树的方法,收集与指定条件匹配的}。在
Tags
和{从基本查找方法:
findAll(name, attrs, recursive, text, limit, **kwargs)
findAll
方法从给定的点开始遍历树,并找到与给定条件匹配的所有Tag
和{findall
方法的签名如下:name
参数可用于传递:关键字参数对标记的属性施加限制。在
搜索具有特定CSS类的标记非常有用,但是CSS属性的名称
class
也是Python的保留字。在您可以使用
soup.find("tagName", { "class" : "cssClass" })
按CSS类进行搜索,就像您给出的代码一样),但是对于这样一个常见的操作,这需要大量的代码。相反,您可以传递attrs
的字符串,而不是字典。在doc还有更多的例子可以帮助您理解。在
相关问题 更多 >
编程相关推荐