Use either html.xpath
and adjust accordingly, or be very implicit when declaring the class to locate. See the following code.
from lxml import html
sample = '<?xml version="1.0" encoding="UTF-8"?><root><a class="asig">I am the correct one.</a><a class="asig drcha">I am the wrong one.</a></root>'
tree = html.fromstring(sample)
print tree.xpath("//a[@class='asig']/text()")[0]
print tree.cssselect("a[class='asig']")[0].text
Result is as follows:
I am the correct one.
I am the correct one.
[Finished in 0.2s]
Notice how cssselect
was used in the last line. Hope this helps.