获取多个值xPath

时间:2014-04-24 09:20:42

标签: python xpath

我不太了解xPath,但我可以编写简单的xpath。

前段时间,我为一个简单的任务编写了一些正则表达式,但现在我感兴趣的是,我如何为此任务编写一个xPath,或者只是说,我如何在一个xpath中选择多个路径... < / p>

<div class="h2"><a href="/?cat=1" title="the cat1">cat1</a></div>
<div>
    <a href="/?to=1321" title="parent cat1">cat1</a>
    <a href="/?to=324"  title="child elem1" class="a2">elem1</a>
    <a href="/?to=33"   title="child elem2" class="a2">elem2</a>

    <a href="/?to=1"    title="parent cat2">cat2</a>
    <a href="/?to=43"   title="child elem1" class="a2">elem1</a>
    <a href="/?to=879"  title="child elem2" class="a2">elem2</a>
    <a href="/?to=768"  title="child elem3" class="a2">elem3</a>

    <a href="/?to=017"  title="parent cat3">cat3</a>
    <a href="/?to=241"  title="child elem1" class="a2">elem1</a>
</div>

我的正则表达式归还了这个。现在我想,xpath返回相同的结果(list&amp; dict - &gt; JSON)。

[
   {
      "href":"/?cat=1",
      "title":"the cat1",
      "anchor":"cat1",
      "childrens":[
         {
            "href":"/?cat=1321",
            "title":"parent cat1",
            "anchor":"cat1",
            "childrens":[
               {
                  "href":"/?to=324",
                  "title":"child elem1",
                  "anchor":"elem1"
               },
               {
                  "href":"/?to=33",
                  "title":"child elem2",
                  "anchor":"elem2"
               }
            ]
         },
         {
            "href":"/?cat=1",
            "title":"parent cat2",
            "anchor":"cat2",
            "childrens":[
               {
                  "href":"/?to=43",
                  "title":"child elem1",
                  "anchor":"elem1"
               },
               {
                  "href":"/?to=879",
                  "title":"child elem2",
                  "anchor":"elem2"
               },
               {
                  "href":"/?to=768",
                  "title":"child elem3",
                  "anchor":"elem3"
               }
            ]
         },
         {
            "href":"/?cat=017",
            "title":"parent cat3",
            "anchor":"cat3",
            "childrens":[
               {
                  "href":"/?to=241",
                  "title":"child elem1",
                  "anchor":"elem1"
               }
            ]
         }
      ]
   }
]

0 个答案:

没有答案