Python - get class from lxml xpath

Using Twitter as simple as, for example, and ignoring the fact that they have a completely useful API, the following script gets the current 5th tweet from the users page.

import urllib2 from lxml import etree xpathselector = "/html/body/div/div[2]/div/div[5]/div[2]/div/ol/li[5]/div/div/p" url = "https://twitter.com/bmthofficial" response = urllib2.urlopen(url) htmlparser = etree.HTMLParser() tree = etree.parse(response, htmlparser) result = tree.xpath(xpathselector) print result[0].text 

And during this message he prints:

From 14.30 you will receive tickets to the Reading Festival and present

Now it prints the contents of <p> / p> how could I, for example, get the name of the class P? HTML code looks like this.

 <p class="js-tweet-text tweet-text">From 2.30pm, win tickets to Reading Festival, and introduce <a dir="ltr" class="twitter-atreply pretty-link" href="/bmthofficial"><s>@</s><b>bmthofficial</b></a> onstage!</p> 

Any help is appreciated! Thanks!

+4
source share
1 answer

Use the get method of Element :

 print result[0].get('class') 

prints

 js-tweet-text tweet-text 
+8
source

Source: https://habr.com/ru/post/1498117/


All Articles