Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.4:href和XPATH_Python_Xpath - Fatal编程技术网

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''')]

更新\勾号已为文本更新\勾号已为文本更新\勾号已为文本更新\勾号已为文本