gpt4 book ai didi

python - 使用 Python 和 BeautifulSoup 解析表

转载 作者:行者123 更新时间:2023-11-30 23:52:12 25 4
gpt4 key购买 nike

我正在尝试使用 Python 和 BeautifulSoup 访问某些 td 标签中的内容。我可以获取第一个符合条件的 td 标签(使用 find),也可以获取所有符合条件的 td 标签(使用 findAll)。

现在,我可以使用 findAll,获取所有内容,并从中获取我想要的内容,但这似乎效率很低(即使我对搜索设置了限制)。有没有办法转到某个符合我想要的标准的 td 标签?说第三个,还是第十个?

这是我到目前为止的代码:

from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function
from mechanize import Browser
from BeautifulSoup import BeautifulSoup

br = Browser()
url = "http://finance.yahoo.com/q/ks?s=goog+Key+Statistics"
page = br.open(url)
html = page.read()
soup = BeautifulSoup(html)
td = soup.findAll("td", {'class': 'yfnc_tablehead1'})

for x in range(len(td)):
var1 = td[x]
var2 = var1.contents[0]
print(var2)

最佳答案

Is there anyway to go to a certain td tag meeting the criteria I want? Say the third, or the 10th?

嗯...

all_tds = [td for td in soup.findAll("td", {'class': 'yfnc_tablehead1'})]

print all_tds[3]

...没有其他办法..

关于python - 使用 Python 和 BeautifulSoup 解析表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6420005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com