Reputation: 4088
I'm sorry if my question isn't formatted right, English isn't my native language.
I'm trying to get the table from the following url Bulapedia, Bulbasaur But lxml gives me very weird results when I use xpath.
I've tried the following:
for elem in tree.xpath('//*[@id="mw-content-text"]//table[14]//tr[3]//td//table//tr//td'):
print(etree.tostring(elem, pretty_print=True))
This doesn't give me the data I need, it gives values from a different table data, randomized even.
I'm at a loss of what to try now, cssselect isn't an option either, since that seems to change depending on which Pokemon I'm searching for.
I'm trying to get the following results:
Upvotes: 1
Views: 55
Reputation: 106425
Other than the first element *[@id="mw-content-text"]
, all the rest of the elements in your XPath should be the immediate children of the ones before them. By using //
you're selecting elements of any depth within the parent, which is not what you want.
Change all but the first //
s to /
and it should work as intended:
for elem in tree.xpath('//*[@id="mw-content-text"]/table[14]/tr[3]/td/table/tr/td'):
print(etree.tostring(elem, pretty_print=True))
Upvotes: 1