欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用 js2xml 获取 script 数据

程序员文章站 2022-05-09 21:16:09
...
import js2xml
from lxml import etree
# script 代码
script = """  $(function () {
        InitZcryListPageLink();
    });
    function InitZcryListPageLink() { 
    var data =
            {
                CurrentPage: 1,
                ItemsPerPage: 10,
                TotalItems: 203,
                TotalPages: 21
            };
        var link = "<td colspan='10' style='text-align:center;color:Red;'>暂无匹配数据</td>";
        if (203 > 0) {
            link = "<td colspan='10' style='text-align:center;'>" + GeneratePagingHtml(data, "LoadZczyryListOfPaging") + "</td>";
        }
        $("#zcrylistpi").html(link);
    }"""
# javascript代码解析,返回一个Element对象
parsed = js2xml.parse(script,encoding='utf-8')
print(parsed)
<Element program at 0x19d2bd094c8>
# 将Element解析为标签形式的代码(类似html标签)
parsed_text = js2xml.pretty_print(parsed)
print(parsed_text)
<program>
  <functioncall>
    <function>
      <identifier name="$"/>
    </function>
    <arguments>
      <funcexpr>
        <identifier/>
        <parameters/>
        <body>
          <functioncall>
            <function>
              <identifier name="InitZcryListPageLink"/>
            </function>
            <arguments/>
          </functioncall>
        </body>
      </funcexpr>
    </arguments>
  </functioncall>
  <funcdecl name="InitZcryListPageLink">
    <parameters/>
    <body>
      <var name="data">
        <object>
          <property name="CurrentPage">
            <number value="1"/>
          </property>
          <property name="ItemsPerPage">
            <number value="10"/>
          </property>
          <property name="TotalItems">
            <number value="203"/>
          </property>
          <property name="TotalPages">
            <number value="21"/>
          </property>
        </object>
      </var>
      <var name="link">
        <string>&lt;td colspan='10' style='text-align:center;color:Red;'&gt;暂无匹配数据&lt;/td&gt;</string>
      </var>
      <if>
        <predicate>
          <binaryoperation operation="&gt;">
            <left>
              <number value="203"/>
            </left>
            <right>
              <number value="0"/>
            </right>
          </binaryoperation>
        </predicate>
        <then>
          <block>
            <assign operator="=">
              <left>
                <identifier name="link"/>
              </left>
              <right>
                <binaryoperation operation="+">
                  <left>
                    <binaryoperation operation="+">
                      <left>
                        <string>&lt;td colspan='10' style='text-align:center;'&gt;</string>
                      </left>
                      <right>
                        <functioncall>
                          <function>
                            <identifier name="GeneratePagingHtml"/>
                          </function>
                          <arguments>
                            <identifier name="data"/>
                            <string>LoadZczyryListOfPaging</string>
                          </arguments>
                        </functioncall>
                      </right>
                    </binaryoperation>
                  </left>
                  <right>
                    <string>&lt;/td&gt;</string>
                  </right>
                </binaryoperation>
              </right>
            </assign>
          </block>
        </then>
      </if>
      <functioncall>
        <function>
          <dotaccessor>
            <object>
              <functioncall>
                <function>
                  <identifier name="$"/>
                </function>
                <arguments>
                  <string>#zcrylistpi</string>
                </arguments>
              </functioncall>
            </object>
            <property>
              <identifier name="html"/>
            </property>
          </dotaccessor>
        </function>
        <arguments>
          <identifier name="link"/>
        </arguments>
      </functioncall>
    </body>
  </funcdecl>
</program>
# 建立xpath树
parsed_tree = etree.HTML(parsed_text)
# 通过xpath获取数据
data = parsed_tree.xpath("//property[@name='TotalPages']/number/@value")[0]
print(data)
21