Reputation: 3215
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
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