Aran Freel
Aran Freel

Reputation: 3215

Python 3.4: href with XPATH

Using lxml and requests I am passing a XPATH to retrieve href attributes of a tags. Every time I use the simple code below I get an AttributeError as exemplified below.

import requests
from lxml import html
import csv




url = 'https://biz.yahoo.com/p/sum_conameu.html'

resp = requests.get(url)
tree = html.fromstring(resp.text)

update_tick = [td.text_content()
               for td in tree.xpath('''//tr[starts-with(normalize-space(.), "Industry")]
                                        /following-sibling::tr[position()>0]
                                        /td/a/@href''')]

print(update_tick)


AttributeError: 'str' object has no attribute 'text_content'

Upvotes: 0

Views: 254

Answers (1)

har07
har07

Reputation: 89295

Passing XPath attribute selector (.../@href) to xpath() method make it return string values of the matched attributes. No need to call text_content() in this case :

update_tick = [td
               for td in tree.xpath('''//tr[starts-with(normalize-space(.), "Industry")]
                                        /following-sibling::tr[position()>0]
                                        /td/a/@href''')]

Upvotes: 3

Related Questions