简体   繁体   English

Python使用lxml xpath从input元素获取值

[英]Python get value from input element with lxml xpath

i want to make paypal cURL login, so i need auth value. 我想要paypal cURL登录,所以我需要auth值。 i try to get auth value from html source with this python code 我尝试使用此python代码从html源获取auth值

import requests
import lxml.html
import StringIO
from xml.etree.ElementTree import ElementTree
r = requests.get("https://paypal.com/cgi-bin/webscr?cmd=_login-run")
login_page = r.text.encode('utf-8') #printing html source
html = lxml.html.fromstring(login_page) #printing <Element html at 0x7f19cb242e$
auth = html.xpath('//input[@name="auth"]') #printing [<InputElement 7fb0971e9f1$
print auth

but the above code printing this [<InputElement 7fb0971e9f18 name='auth' type='hidden'>] , so how i can get auth value with decoded &#x2d; &#x2d; &#x2e; 但是上面的代码打印了这个[<InputElement 7fb0971e9f18 name='auth' type='hidden'>] ,所以我如何通过解码&#x2d; &#x2d; &#x2e;获取auth值&#x2d; &#x2d; &#x2e; &#x2d; &#x2d; &#x2e; ? the input section looks like this 输入部分看起来像这样

<input name="auth" type="hidden" value="ADPifNsidn&#x2d;P0G6WmiMMeJbjEhnhIvZCNg7Fk11NUxc0DyYWzrH&#x2d;xk5ydV&#x2e;85WCzy">

thanks you very much. 非常感谢你。

If you'd like to retrieve the auth attribute of that element, use 如果您想要检索该元素的auth属性,请使用

auth = html.xpath('//input[@name="auth"]/@value')

there is no need to decode anything, entities are expanded automatically when lxml parses HTML, and therefore the output will be 没有必要解码任何东西,当lxml解析HTML时,实体会自动扩展,因此输出将是

$ python sample.py 
['AmmyYuqCDmZRcSs6MaQi2tKhzZiyAX0eSERKqTi3pLB5pdceB726lx7jhXU2MGDN6']

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

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