我用 python 编写了一个抓取工具来解析网页中的一些数据。我的目的是将数据存储在字典中。我没有演示完整的表格,而是尝试使用包含单个玩家信息的单个 tr
。数据正在通过,但输出的格式不是字典的样子。任何使其准确的帮助都将受到高度赞赏。
这是我的尝试:
import requests
from bs4 import BeautifulSoup
URL = "https://fantasy.premierleague.com/player-list/"
def get_data(link):
res = requests.get(link,headers={"User-Agent":"Mozilla/5.0"})
soup = BeautifulSoup(res.text,"lxml")
data = []
for content in soup.select("div.ism-container"):
itmval = {}
itmval['name'] = content.select_one("h2").text
itmval['player_info'] = [[item.get_text(strip=True) for item in items.select("td")] for items in content.select(" table:nth-of-type(1) tr:nth-of-type(2)")]
data.append(itmval)
print(data)
if __name__ == '__main__':
get_data(URL)
我的输出:
[{'name': 'Goalkeepers', 'player_info': [['De Gea', 'Man Utd', '161', '£5.9']]}]
我期望的输出:
[{'name': 'Goalkeepers', 'player_info': ['De Gea', 'Man Utd', '161', '£5.9']}]
顺便说一句,我打算解析完整的表格,但我在这里展示了一个最小部分,以便您仔细观察。
最佳答案
如果你想使用嵌套列表理解,请尝试替换
[[item.get_text(strip=True) for item in items.select("td")] for items in content.select(" table:nth-of-type(1) tr:nth-of-type(2)")]
与
[item.get_text(strip=True) for items in content.select(" table:nth-of-type(1) tr:nth-of-type(2)") for item in items.select("td")]
关于python - 无法获取格式正确的字典输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50009153/