Reputation: 154
I'm trying to write a Python code that would visit the page of Real Madrid on Wikipedia and would print the names of its squad
My current xpath query is:
for t in doc.xpath("//table//table/tr[position() > 1]/td[4]/span//text()"):
#print the player's name here
But this also prints the players that are in the "Out on loan" table.
So my question is how do I select only the first table that contains a table in the xpath query? or maybe there's another way to achieve what I want?
Thank you very much.
P.S: The table starts at line 775 in the view-source of Real Madrid's page on Wikipedia (https://en.wikipedia.org/wiki/Real_Madrid_C.F.).
Upvotes: 2
Views: 329
Reputation: 12623
You can use the following XPath:
(//span[@id='Current_squad']/following::table)[1]
This will select only the "Current sqaud" table.
To get the list of the players, you can use the following XPath:
(//span[@id='Current_squad']/following::table)[1]//span[@class='fn']//text()
Upvotes: 2