<p id="g32nn"></p>
    1. <acronym id="g32nn"><strong id="g32nn"></strong></acronym>
      <pre id="g32nn"></pre>

      <table id="g32nn"><option id="g32nn"></option></table>

          Python爬蟲-Xpath語法與lxml庫的用法(二)
          2022-08-29 23:54:55

          一、 安裝

          pip方式安裝

          pip install lxml

          二、 Xpath術語

          2.1 節點

          在 XPath 中,有七種類型的節點:元素、屬性、文本、命名空間、處理指令、注釋以及文檔(根)節點。XML 文檔是被作為節點樹來對待的。樹的根被稱為文檔節點或者根節點。
          請看下面這個 XML 文檔:

          <?xml version="1.0" encoding="ISO-8859-1"?>
          
          <bookstore>
          
          <book>
            <title lang="en">Harry Potter</title>
            <author>J K. Rowling</author> 
            <year>2005</year>
            <price>29.99</price>
          </book>
          
          </bookstore>
          

          上面的XML文檔中的節點例子:

          <bookstore> (根節點)
          <author>J K. Rowling</author> (元素節點)
          lang="en" (屬性節點) 
          

          2.2 節點關系

          父(Parent)
          每個元素以及屬性都有一個父。
          在下面的例子中,book 元素是 title、author、year 以及 price 元素的父:

          <book>
            <title>Harry Potter</title>
            <author>J K. Rowling</author>
            <year>2005</year>
            <price>29.99</price>
          </book>
          

          子(Children)
          元素節點可有零個、一個或多個子。
          在下面的例子中,title、author、year 以及 price 元素都是 book 元素的子:

          <book>
            <title>Harry Potter</title>
            <author>J K. Rowling</author>
            <year>2005</year>
            <price>29.99</price>
          </book>
          

          兄弟(Sibling)

          擁有相同的父的節點
          在下面的例子中,title、author、year 以及 price 元素都是兄弟:

          <book>
            <title>Harry Potter</title>
            <author>J K. Rowling</author>
            <year>2005</year>
            <price>29.99</price>
          </book>
          

          先輩(Ancestor)
          某節點的父、父的父,等等。
          在下面的例子中,title 元素的先輩是 book 元素和 bookstore 元素:

          <bookstore>
          
          <book>
            <title>Harry Potter</title>
            <author>J K. Rowling</author>
            <year>2005</year>
            <price>29.99</price>
          </book>
          
          </bookstore>
          

          后代(Descendant)
          某個節點的子,子的子,等等。
          在下面的例子中,bookstore 的后代是 book、title、author、year 以及 price 元素:

          2.3 選取節點

          XPath 使用路徑表達式在 XML 文檔中選取節點。節點是通過沿著路徑或者 step 來選取的。
          下面列出了最有用的路徑表達式:


          表達式

          描述
          nodename 選取此節點的所有子節點
          / 從根節點選取
          // 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。
          . 選取當前節點
          選取當前節點的父節點
          @ 選取屬性

          實例

          在下面的表格中,我們已列出了一些路徑表達式以及表達式的結果:

          謂語(Predicates)
          謂語用來查找某個特定的節點或者包含某個指定的值的節點。
          謂語被嵌在方括號中。

          實例

          在下面的表格中,我們列出了帶有謂語的一些路徑表達式,以及表達式的結果:

          選取未知節點
          XPath 通配符可用來選取未知的 XML 元素。

          ?在下面的表格中,我們列出了一些路徑表達式,以及這些表達式的結果:

          選取若干路徑
          通過在路徑表達式中使用“|”運算符,您可以選取若干個路徑。

          2.4 XPath 實例

          # -*- coding: utf-8 -*-
          
          from lxml import etree
          
          
          text = """
          <div class="wrapper">
              <i class="iconfont icon-back" id="back"></i>
              <a rel="nofollow" href="/" id="channel">新浪社會</a>
              <ul id="nav">
                  <li><a rel="nofollow"  title="國內">國內</a></li>
                  <li><a rel="nofollow"  title="國際">國際</a></li>
                  <li><a rel="nofollow"  title="軍事">軍事</a></li>
                  <li><a rel="nofollow"  title="圖片">圖片</a></li>
                  <li><a rel="nofollow"  title="社會">社會</a></li>
                  <li><a rel="nofollow"  title="娛樂">娛樂</a></li>
                  <li><a rel="nofollow"  title="科技">科技</a></li>
                  <li><a rel="nofollow"  title="體育">體育</a></li>
                  <li><a rel="nofollow"  title="財經">財經</a></li>
                  <li><a rel="nofollow"  title="汽車">汽車</a></li>
              </ul>
              <i class="iconfont icon-liebiao" id="menu"></i>
          </div>
          """
          
          # 創建html對象
          html = etree.HTML(text)
          
          # 獲取所有a標簽的href內容
          results = html.xpath('//a/@href')
          print(results)
          # 獲取所有id為channel的a標簽的href內容
          results2 = html.xpath('//a[@id="channel"]/@href')
          print(results2)
          # 獲取所有li標簽下的a標簽的文本內容
          results3 = html.xpath('//li/a/text()')
          print(results3)
          

          運行結果如下:

          ['/', 'http://domestic.firefox.sina.com/', 'http://world.firefox.sina.com/', 'http://mil.firefox.sina.com/', 'http://photo.firefox.sina.com/', 'http://society.firefox.sina.com/', 'http://ent.firefox.sina.com/', 'http://tech.firefox.sina.com/', 'http://sports.firefox.sina.com/', 'http://finance.firefox.sina.com/', 'http://auto.firefox.sina.com/']
          ['/']
          ['國內', '國際', '軍事', '圖片', '社會', '娛樂', '科技', '體育', '財經', '汽車']
          

            

          備注:

          Ranorex Selocity 類似firepath的chrome插件  

          http://crx4.com/8198.html

          ?

          ?

            

            

            

            

            

          本文摘自 :https://www.cnblogs.com/


          更多科技新聞 ......

          97久久久久人妻精品专区_国产成人精品视频导航_国产色诱视频在线播放网站_97午夜理论电影影院
          <p id="g32nn"></p>
          1. <acronym id="g32nn"><strong id="g32nn"></strong></acronym>
            <pre id="g32nn"></pre>

            <table id="g32nn"><option id="g32nn"></option></table>