美丽的汤循环给我个人名单,但在需要一个数据框架

2024-04-20 03:25:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用漂亮的soup来废弃数据,但是它是以列表的形式出现的,但是我需要一个pandas数据框架。我使用for循环来获取数据,但是我无法将它们附加到dataframe。当我查一查第二排的长度时,它只显示1。你知道吗

INFY = url.urlopen("https://in.finance.yahoo.com/quote/INFY.NS/history?p=INFY.NS")
div = INFY.read()
div = soup(div,'html.parser')
div = div.find("table",{"class":"W(100%) M(0)"})
table_rows = div.findAll("tr")
print(table_rows)

for tr in table_rows:
    td = tr.findAll('td')
    row = [i.text for i in td]
    print(row)

下面是我运行代码后得到的结果。你知道吗

['30-Mar-2017', '1,034.00', '1,035.90', '1,020.25', '1,025.50', '1,010.02', '60,78,590']
['29-Mar-2017', '1,034.30', '1,041.50', '1,025.85', '1,031.85', '1,016.27', '34,90,593']
['28-Mar-2017', '1,031.50', '1,039.00', '1,030.05', '1,035.15', '1,019.52', '23,98,398']

Tags: 数据indivfortabletrmarrows