【发布时间】:2018-07-03 23:27:09
【问题描述】:
在我的下表中,我已抓取项目 1-4 并将它们存储在名为标题的变量中。
我还想选择值 1-4 并将它们存储在一个名为列的变量中,无论如何都要选择每一秒。像
columns = boxinfo.find_all("td").nthChild(2)
我正在从中抓取的表结构
<div class="box1">
<table class="table1">
<tr><td class="label">Item1</td><td>Value1</td></tr>
<tr><td class="label">Item2</td><td>Value2</td></tr>
<tr><td class="label">Item3</td><td>Value3</td></tr>
<tr><td class="label">Item4</td><td>Value4</td></tr>
</table>
</div>
代码
#Find our information
boxinfo = soup.find("div", {"id": "box1"})
headings = boxinfo.find_all("td", {"class": "label"})
columns = boxinfo.find_all("td").nthChild(2) #This does not work :(
【问题讨论】:
-
纯美的汤是你做不到的,但你可以在搜索中添加过滤功能。但最简单的是
columns = [column for i, column in enumerate(boxinfo.find_all("td")) if i%2 == 1] -
@bobrobbob 我如何获得以下html元素,因为我在python中使用带有beautifulsoup的js来获取它-- document.querySelector('body > table > tbody > tr > td > table > tbody > tr:nth-child(2) > td:nth-child(2) > div:nth-child(3) > table > tbody > tr:nth-child(11) > td > table > tbody > tr:nth- child(4) > td:nth-child(5) > input[type="hidden"]:nth-child(1)').getAttribute("name")
标签: python beautifulsoup