Python 3.4:href和XPATH
使用Python 3.4:href和XPATH,python,xpath,Python,Xpath,使用lxml和请求我正在传递一个XPATH来检索a标记的href属性。每次使用下面的简单代码,我都会得到一个AttributeError,如下所示 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 =
lxml
和请求
我正在传递一个XPATH
来检索a
标记的href
属性。每次使用下面的简单代码,我都会得到一个AttributeError
,如下所示
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'
将XPath属性选择器(
../@href
)传递给XPath()
方法,使其返回匹配属性的字符串值。在这种情况下,无需调用text\u content()
:
update_tick = [td
for td in tree.xpath('''//tr[starts-with(normalize-space(.), "Industry")]
/following-sibling::tr[position()>0]
/td/a/@href''')]
将XPath属性选择器(
../@href
)传递给XPath()
方法,使其返回匹配属性的字符串值。在这种情况下,无需调用text\u content()
:
update_tick = [td
for td in tree.xpath('''//tr[starts-with(normalize-space(.), "Industry")]
/following-sibling::tr[position()>0]
/td/a/@href''')]
将XPath属性选择器(
../@href
)传递给XPath()
方法,使其返回匹配属性的字符串值。在这种情况下,无需调用text\u content()
:
update_tick = [td
for td in tree.xpath('''//tr[starts-with(normalize-space(.), "Industry")]
/following-sibling::tr[position()>0]
/td/a/@href''')]
将XPath属性选择器(
../@href
)传递给XPath()
方法,使其返回匹配属性的字符串值。在这种情况下,无需调用text\u content()
:
update_tick = [td
for td in tree.xpath('''//tr[starts-with(normalize-space(.), "Industry")]
/following-sibling::tr[position()>0]
/td/a/@href''')]
更新\勾号已为文本更新\勾号已为文本更新\勾号已为文本更新\勾号已为文本