Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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查找给定源的视图状态值_Python_Regex_String_Viewstate - Fatal编程技术网

Python查找给定源的视图状态值

Python查找给定源的视图状态值,python,regex,string,viewstate,Python,Regex,String,Viewstate,我试图写一个程序来解码给定url的视图状态。我知道存在类似的项目,但这与其说是一个项目,不如说是一次短途旅行。 给定页面的html源代码,如何获取视图状态表单元素的值。 我从这样做开始: def get_viewstate(html): i = html.index('id="__VIEWSTATE" value="') somedata = html[i+len('id="__VIEWSTATE" value="'):] 但我无法找到一种有效的方法,只检索元素

我试图写一个程序来解码给定url的视图状态。我知道存在类似的项目,但这与其说是一个项目,不如说是一次短途旅行。 给定页面的html源代码,如何获取视图状态表单元素的值。 我从这样做开始:

def get_viewstate(html):
        i = html.index('id="__VIEWSTATE" value="')
        somedata = html[i+len('id="__VIEWSTATE" value="'):]
但我无法找到一种有效的方法,只检索元素的值直到结束标记。 检索此表单元素值的最有效方法是什么?

使用with css选择器:

import lxml.html

root = lxml.html.fromstring(html)
matched = root.cssselect('#__VIEWSTATE')
if matched:
    value = matched[0].get('value')
使用:


最好的方法是解析HTML。就性能而言,“最有效的方法”显然是您使用的方式。但是,您可以用它的值替换
len('id=“\uu VIEWSTATE”value=“”)
,因为它是一个固定长度的字符串。需要“#”吗?@735Tesla,是的,它是必要的。
\uu VIEWSTATE
是css选择器。(相当于
*[id=“\uu VIEWSTATE]”
)。
from bs4 import BeautifulSoup

soup = BeautifulSoup(html)
matched = soup.select('#__VIEWSTATE')
if matched:
    value = matched[0].get('value')