【发布时间】:2016-01-13 13:49:22
【问题描述】:
要真正正确处理这些数据,我希望能够在 Python 中制作嵌套汤。
因此,例如在 .Net 中,我会执行以下操作:
For Each node As HtmlNode In document.DocumentNode.SelectNodes("//tr[@class='ClassName']")
For Each SecondNode As HtmlNode In node.SelectNodes(".//span[@class='SecondClassName']")
所以我可以解析每个部分中的元素,我已经用 BeautifulSoup 找到了。
我正在尝试这样的事情:
soup = BeautifulSoup(WebDriver.page_source, "html5lib")
for EachSection in soup.find_all("tr", {"class" : "ClassName"}):
soup2 = BeautifulSoup(EachSection, "html5lib")
print soup2
第一部分有效。我可以使用 print 转储 EachSection 的代码或使用 get_text() 获取内容,但第二部分会导致问题。
它抛出:
TypeError: 'NoneType' object is not callable
我知道我可以将第一个汤直接发送到 Span/SecondClassName 但它会读取 .Net 不会读取的换行符,我可能不得不使用一些容易出错的解析来获得我需要的一切。
有没有办法做到这一点?
/编辑:
我一直在玩各种变化:
if EachSection.parent.name == 'div':
print EachSection["tr"]
甚至不知道我是否在这里寻找正确的地方还没有让它工作。不过,这似乎是一种更整洁的做事方式。
【问题讨论】:
-
为什么能正确处理数据?
标签: python web-scraping beautifulsoup