Friday, 15 March 2013

XPath how to get Python -



XPath how to get Python -

i'm trying xpath text in illustration below "7061 main st"

<td rowspan="2"> <font face="arial,helvetica" size="-1"> 7061 main st </font> </td>

however it's not working me. tried next below , won't work. on search in source that's 1 has attribute rowspan="2"

searchresults = tree.xpath('//*[@rowspan="2"]/@text') self.response.out.write(searchresults) searchresults = tree.xpath('//*[@rowspan="2"]/font/@text') self.response.out.write(searchresults) searchresults = tree.xpath('//*[@rowspan="2"]/font[text()]') self.response.out.write(searchresults)

what shoudl text?

thanks!

searchresults = tree.xpath('//td[@rowspan="2"]/font/text()')

will create searchresults equal list

['\n\n\n 7061 main st\n\n ']

(note may want utilize str.strip method remove whitespace both ends of string.)

@text refers attribute text. example, rowspan attribute of td, , face attribute of font. here, want actual text, not attribute. utilize text() instead.

also, if omit font xpath, in

//td[@rowspan="2"]/text()

then retrieving text associated td tag. empty in html posted. want text associated font tag, include font in xpath:

//td[@rowspan="2"]/font/text() finally, know brackets [...] indicate "such that" relationship in xpath. example, td[@rowspan="2"] matches td tags such that rowspan attribute equals "2". font[text()] matchs font tags such that contains text(). returns font tag itself. since want text, not tag, utilize font/text() instead of font[text()].

python xpath

No comments:

Post a Comment