简体   繁体   English

通过python中的xpath提取url源的值

[英]Extracting value of url source by xpath in python

This is my page content: 这是我的页面内容:

<input type="hidden" name="frm-id" value="AAA" id="frm-id" /></form></div><div id="container-getfocus_AAA" style="display:none"><input type="text" id="getfocus_txt_AAA" name="getfocus_txt_AAA" /></div>               <script type="text/javascript">formtarget['AAA'] = '';</script></div></div></div></div>    <!--        </div>-->

I want to extract AAA from value : 我想从value提取AAA

from lxml import html
import requests
cont=request.get(url).content
tree=html.fromstring(cont)
print tree.xpath('//input[@name="frm-id"].text()')

Output is:
  File "<stdin>", line 1, in <module>
  File "lxml.etree.pyx", line 1509, in lxml.etree._Element.xpath (src/lxml/lxml.etree.c:50702)
  File "xpath.pxi", line 318, in lxml.etree.XPathElementEvaluator.__call__ (src/lxml/lxml.etree.c:145954)
  File "xpath.pxi", line 238, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:144962)
  File "xpath.pxi", line 224, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:144817)
lxml.etree.XPathEvalError: Invalid expression

Your XPath expression is not valid. 您的XPath表达式无效。 The correct XPath to get value attribute of input tag is as follow : 获取input标签的value属性的正确XPath如下:

//input[@name="frm-id"]/@value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM