python - AttributeError: 'unicode' object has no attribute 'xpath issue -


i trying capture value in "//html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td[17]/font" through xpath. not sure wrong doing when running below code getting error message "attributeerror: 'unicode' object has no attribute 'xpath'" can please me

import smtplib import requests #import bs4 lxml import html email.mime.text import mimetext   def login():         url = "http://172.16.3.16/bkg/nimble/newsite_airfail_isimba_dom.php"         r = requests.get(url, auth=('stats', 'stats'))         page = r.text         return page   def extractfailure():         loginpage = login()         fail = loginpage.xpath('/html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td[17]/font')         print fail  if __name__ == '__main__':         extractfailure() 

you appear have forgotten parse response body.

you need use lxml.html parser somewhere before can use xpath expressions:

def extractfailure():     loginpage = html.fromstring(login())     fail = loginpage.xpath('/html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td[17]/font')     print fail 

take account browsers insert <tbody> elements if missing document. lxml not insert these, browser-sourced xpath expression may wrong.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -