受此解決方案的啟發,我一直在使用以下代碼來清理我使用 Beautiful Soup 獲得的一些資料:
nfl = soup.findAll('li', "player")
lines = ("{}. {}\n".format(ind,span.get_text(strip=True).rstrip(" "))
for ind, span in enumerate(nfl,1))
print("".join(lines))
問題是它的輸出以字串的格式出現,我想將它的每一行存盤為資料框中的不同行。我嘗試在回圈中引入代碼,但那不行。我能做到的最好的是將相同的字串 n 次存盤到我想要的資料框中。你能幫幫我嗎?
uj5u.com熱心網友回復:
嘗試:
nfl = soup.findAll("li", "player")
all_data = []
for span in nfl:
all_data.append({"player": span.get_text(strip=True).rstrip(" ")})
df = pd.DataFrame(all_data)
print(df)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520259.html
