[英]How can I fix this python code to get all of the names of the people in the table I am referencing in the code?
I am trying to get python to give me the names of the State Senators from Alabama on Ballotpedia.我试图让 python 在 Ballotpedia 上给我来自阿拉巴马州的州参议员的名字。 However, the code I put together is only giving me the title I requested from the url but I am not getting any names.但是,我放在一起的代码只给了我从 url 请求的标题,但我没有得到任何名字。 Here is my current python code:这是我当前的python代码:
import requests
from bs4 import BeautifulSoup
import pandas as pd
list = ['https://ballotpedia.org/Alabama_State_Senate']
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
jquery-tablesorter a")]
df = pd.DataFrame.from_dict(temp_dict,
orient='index').transpose()
I believe my error is in this line:我相信我的错误在这一行:
temp_dict[page.split('/')[-1]] = [item.text for item in soup.select("table.bptable gray sortable tablesorter jquery-tablesorter a")]
Thank you.谢谢你。
This seems to work for me:这似乎对我有用:
import requests
from bs4 import BeautifulSoup
url = "https://ballotpedia.org/Alabama_State_Senate"
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.content, "html.parser")
for row in soup.find(id="officeholder-table").select("tr:not([colspan])"):
name = row.select_one("td:nth-of-type(2)").text
print(name)
Another way to get all info:获取所有信息的另一种方法:
holders = soup.select("#officeholder-table")
targets = holders[0].select('tr')
for target in targets:
print(target.text)
Output:输出:
Alabama State Senate District 20
Linda Coleman-Madison
Democratic
2006
etc.等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.