我正在尝试从以下位置抓取特定的表格:这个网页 http://biz.yahoo.com/c/s.html
我想要抓取的是股票信息。日期、公司名称、比例以及是否可选。
这是我到目前为止所拥有的:
from bs4 import BeautifulSoup
import urllib2
url = "http://biz.yahoo.com/c/s.html"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
alltables = soup.find_all('table')
这段代码为我提供了页面上的所有表格(不止一个)。
1)我不知道如何识别我需要的表。
2)我不确定如何将该表中的信息提取到数组或列表或其他可用于进一步分析的数据结构中。
标记并不容易抓取 - 没有id
或具体class
您可以使用这些属性来区分各个表。在这种情况下我会做的是find a Payable
标题单元格并找到第一个table
parent http://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-parents-and-find-parent:
header = soup.find("b", text="Payable")
table = header.find_parent("table")
然后,您可以迭代表行,跳过前 2 个标题和带有分隔符的行:
for row in table.find_all("tr")[2:]:
print([cell.get_text(strip=True) for cell in row.find_all("td")])
并且,您可以将其转换为列表的列表:
[[cell.get_text(strip=True)
for cell in row.find_all("td")]
for row in table.find_all("tr")[2:]]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)