【发布时间】:2020-08-12 06:56:52
【问题描述】:
我正在尝试迭代某个足球队的实际队员。 我注意到在维基百科中属于球队的球员有相同的格式。 这种格式有 4-6 张桌子,其中 2 张给一线队球员,剩下的给租借球员或年轻球员等...... 当使用在线工具使用 XPath 查询查询维基百科页面时,我得到了我想要的结果,但是当我将它与 Python lxml.html 库和请求库一起使用时,而不是将玩家的表格视为 4-6 个表格,它会看到它作为一个表格元素,只提取一队球员很头疼。
这是我的python代码:
def create_team_ontology(ontology_graph,team_url,team_name):
res = requests.get(team_url)
doc = lxml.html.fromstring(res.content)
print(team_url)
club_players = doc.xpath("//table[3]/tbody//tr[position() > 1]//td[4]//span/a/@href")
for player_suffix_url in club_players:
print(player_suffix_url+'\n')
player_url = wiki_prefix + player_suffix_url
get_player_info(ontology_graph,player_url,team_name)
这里是阿森纳https://en.wikipedia.org/wiki/Arsenal_F.C 的维基页面示例。 在源文件中很容易检查每个表是不同的元素。 但是我的俱乐部球员列表包含上述页面中球员类别下的所有球员href。
这是我在网络上运行的代码,使用检查然后 ctrl+f //table[3]/tbody//tr[position() > 1]//td[4]//span/a/@href
【问题讨论】:
-
您能否发布代码,通过 XPATH 查询返回您想要的内容?
-
@Jortega 是的,我已经编辑过了,请注意我已经编辑了我的 python 代码和 wiki 链接,以明确它是同一个查询。