这里是python的新手,我有一个关于用漂亮的汤从刮痕中创建桌子的问题。以下是我使用的代码:
import requests
page=requests.get("https://www.opensecrets.org/lobby/lobbyist.php?id=Y0000008510L&year=2018")
from bs4 import BeautifulSoup
soup=BeautifulSoup(page.content, 'lxml')
table=soup.find(‘table’,{‘id’:’lobbyist_summary’})
for row in table:
cells=row.find_all(‘a’)
rn=cells[0].get_text()
错误是:
^{pr2}$打印(表格)如下所示:
[<a href="firmsum.php?id=D000037635&year=2018">Ballard Partners</a>, <a href="clientsum.php?id=F203227&year=2018">Advanced Roofing Inc</a>, <a href="clientsum.php?id=F214670&year=2018">Africell Holding</a>, <a href="clientsum.php?id=D000023883&year=2018">Amazon.com</a>, ...]
我希望(最终)得到一个表,该表将每个感兴趣的元素放在一个单独的列中,这样它看起来像:
[[firmsum,D0000376352018,Ballard Partners],[clientsum,F20322722018,Advanced Roofing Inc],[clientsum,F2146702018,Africell控股],[clientsum,D0000238832018,亚马逊网站]…]
分配4个空列表:
首先,让我们得到第4列的值:
^{pr2}$这样可以得到:
{1>现在从第一列中提取值:
现在,让我们将所有列表放入一个数据框中,使其看起来整洁:
如果我输出前几行,它看起来就像您想要的那样:
相关问题 更多 >
编程相关推荐