我正在尝试使用 beautiful soup 来废弃数据,但是它以列表的形式出现,但是我需要一个 pandas 数据框。我正在使用 for 循环来获取数据,但是我无法将这些数据附加到数据帧中。当我检查行的长度时,它只显示 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']
最佳答案
pd.DataFrame([[i.text for i in tr.findAll('td')] for tr in table_rows])
然后,您需要将文本值转换为其等效数字。
关于python - Beautiful Soup For 循环给了我单独的列表,但是需要一个数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45495054/