我试图使用此代码从BallotMedia(https://ballotpedia.org/Governor_(state_executive_office))中获取信息,特别是高管的姓名。我这里的代码只提供以下输出:
,Governor_(state_executive_office),Lieutenant_Governor_(state_executive_office),Secretary_of_State_(state_executive_office),Attorney_General_(state_executive_office)
我也在设法弄到那些名字。这是我目前的代码:
import requests
from bs4 import BeautifulSoup
import pandas as pd
list = ['https://ballotpedia.org/Governor_(state_executive_office)', 'https://ballotpedia.org/Lieutenant_Governor_(state_executive_office)', 'https://ballotpedia.org/Secretary_of_State_(state_executive_office)', 'https://ballotpedia.org/Attorney_General_(state_executive_office)']
temp_dict = {}
for page in list:
r = requests.get(page)
soup = BeautifulSoup(r.content, 'html.parser')
temp_dict[page.split('/')[-1]] = [item.text for item in
soup.select("table.bptable.gray.sortable.tablesorter
tablesorter-default tablesorter17e7f0d6cf4b4 jquery-
tablesorter")]
最后一行是我认为存在问题的那一行。我已尝试删除并向“table.bptable.gray.sortable.tablesorter tablesorter default tablesorter17e7f0d6cf4b4 jquery tablesorter”部分添加代码,但始终得到相同的结果。我直接从网站上复制的不我不确定我遗漏了什么。如果不是这样,那么该行中的其余代码是否有问题?谢谢大家!
首先使用以下css选择器查找表,然后使用
pandas
读取_html() 并加载到数据帧中。 这将在单个数据帧中提供所有数据如果要获取单个数据帧,请尝试此操作
有一种更简单的方法。随机选取一个URL,尝试以下操作:
输出:
等等
您可以尝试通过选择器访问表格:
相关问题 更多 >
编程相关推荐