Href attribute for lxml.html

according to this answer :

>>> from lxml.html import fromstring
>>> s = """<input type="hidden" name="question" value="1234">"""
>>> doc = fromstring(s)
>>> doc.value
'1234'
>>> doc.name
'question'

      

I tried to get both link and text from this code:

from lxml.html import fromstring
s = '<a href="http://a.com" rel="bookmark">bla bla bla</a>'
doc = fromstring(s)
print (doc.href)
print (doc.text_content())

      

He gives AttributeError:'HtmlElement' object has no attribute 'href'

Im new in lxml. What was the real problem?

How can I use both link (a.com) and text (bla bla bla) as lines from this code?

+3


source to share


1 answer


This code works for me

from lxml.html import document_fromstring
doc = document_fromstring('<a href="http://a.com" rel="bookmark">bla bla bla</a>')
print (doc.xpath("//a")[0].get("href"))
print (doc.text_content())

      



Output:

http://a.com
bla bla bla

      

+3


source







All Articles