python簡單使用xpath查找網頁元素


 
         

xPath
一種HTML和XML的查詢語言,他能在XML和HTML的樹狀結構中尋找節點

安裝
pip install lxml

 
         

HTML
超文本標記語言,是一種規范,一種標注,是構成網頁文檔的主要語言

URL
統一資源定位器,互聯網上的每個文件都有一個唯一的URL,它包含的信息指出文件的位置
以及瀏覽應該怎么處理它。

xPath的使用
獲取文本
//標簽1[@屬性1="屬性值1"]/標簽2[@屬性2="屬性值"]/..../text()

獲取屬性值
//標簽1[@屬性1="屬性值1"]/標簽2[@屬性2="屬性值"]/..../@屬性n

所需要的html文檔
<!DOCTYPE html>
<html>
    <head lang="en">
        <meta charset="UTF-8">
        <title>注冊頁面</title>
        <style>
            table{
                border: 1px solid beige;
            }
            span{
                color: #66CCFF;
            }
            th{
                background-color: gainsboro;
            }
            tr{
                border: 1px solid beige;
            }
            td{
                border: 1px solid aliceblue;
            }
            .redText{
                color: red;
            }
            .redStar{
                color: red;
            }
        </style>
    </head>
    <body>
            <p>
            開始
        </p>
        <!-- 8列-->
        <h3>標題,我是一個大帥哥</h3>
        <ul>
            <li>內容1</li>
             <li>內容2</li>
             <li class="important">內容3important</li>
             <li>內容4</li>
             <li>內容5</li>
        </ul>
                    <p>
            中間
        </p>
        <div id = "container">
            段落文字
            <a href="http://www.baidu.com" title="超鏈接">跳轉到百度首頁</a>
        </div>
        <p>
            最后
        </p>
        <form action="">
            <table align="center" cellspacing="0">
                <tr>
                    <th colspan="8" align="left">1.會員登錄名和密碼</th>
                </tr>
                <tr>
                    <td>用戶名:</td>
                    <td colspan="2"><input type="text"/><span class="redStar">*&emsp;</span></td>
                    <td colspan="5"><input type="button" value="檢測用戶名"/><span>5-15位,請使用英文(a-z、A-Z)、數字(0-9)</span></td>
                </tr>
                <tr>
                    <td>&emsp;碼:</td>
                    <td colspan="2"><input type="password"/><span class="redStar">*&emsp;</span></td>
                    <td colspan="5"><span>5-15位,請使用英文(a-z)、數字(0-9)注意區分大小寫;<br/>密碼不能與登錄名相同;易記;難猜;</span></td>
                </tr>
                <tr>
                    <td>再次輸入密碼:</td>
                    <td colspan="2"><input type="password"/><span class="redStar">*&emsp;</span></td>
                    <td colspan="5"><span>兩次輸入的密碼必須一致</span></td>
                </tr>
                <tr>
                    <th colspan="8" align="left">2.姓名和聯系方式</th>
                </tr>
                <tr>
                    <td>真實姓名:</td>
                    <td colspan="2"><input type="text"/><span class="redStar">*&emsp;</span></td>
                    <td colspan="5"><input type="radio" checked="checked" name="Sex" value="male"/>先生<input type="radio" name="Sex" value="female"/>小姐</td>
                </tr>
                <tr>
                    <td>電子郵箱:</td>
                    <td colspan="2"><input type="text"/><span class="redStar">*&emsp;</span></td>
                    <td colspan="5"><span class="redText">非常重要!</span><br/><span>這是客戶與您聯系的首選方式,請一定填寫真實。</span></td>
                </tr>
                <tr>
                    <td>固定電話:</td>
                    <td colspan="2"><input type="text"/><span class="redStar">*&emsp;</span></td>
                    <td colspan="5"><span>區號+電話號碼</span></td>
                </tr>
                <tr>
                    <td>公司所在地:</td>
                    <td colspan="7">
                        <select>
                            <option>北京</option>
                            <option>上海</option>
                            <option>成都</option>
                        </select>
                        <select>
                            <option>東城
                            </option>
                            <option>武侯區</option>
                            <option>金牛區</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>街道地址:</td>
                    <td colspan="5"><input style="width: 350px;" type="text"/><span class="redStar">*&emsp;</span></td>
                    <td colspan="2"><span>填寫縣(區)、街道、門牌號</span></td>
                </tr>
                <tr>
                    <td>傳真號碼:</td>
                    <td colspan="7"><input type="text"/></td>
                </tr>
                <tr>
                    <td>手機號碼:</td>
                    <td colspan="7"><input type="text"/></td>
                </tr>
                <tr>
                    <td>郵政編碼:</td>
                    <td colspan="7"><input type="text"/></td>
                </tr>
                <tr>
                    <th colspan="8" align="left">3.公司名稱和主營業務</th>
                </tr>
                <tr>
                    <td>貴公司名稱:</td>
                    <td colspan="2"><input type="text"/><span class="redStar">*&emsp;</span></td>
                    <td colspan="5"><span>請填寫在工商注冊的公司/商號全稱;<br/>無商號的個體經營者填寫執照上的姓名,如:張三(個體經營)</span></td>
                </tr>
                <tr>
                    <td>你的職位:</td>
                    <td colspan="2"><input type="text"/><span class="redStar">*&emsp;</span></td>
                    <td colspan="5"></td>
                </tr>
                <tr>
                    <td>主營行業:</td>
                    <td colspan="2">
                        <select>
                            <option>電子電工</option>
                            <option>IT科技</option>
                            <option>小豬佩奇</option>
                        </select>
                    </td>
                    <td colspan="5"><span>請正確選擇。您會收到該行業、該產品的供求信息</span></td>
                </tr>
                <tr>
                    <td>主營產品/服務:</td>
                    <td colspan="7"><input  style="width: 250px;" type="text"/><span class="redStar">*&emsp;</span><span>3個主要相關品名/服務名,最少要填一個。例如:太陽帽,布料,拉鏈</span>
                    </td>
                </tr>
                <tr>
                    <td>公司地址:</td>
                    <td colspan="7"><input  style="color: darkgrey;width: 250px;" type="text" value="http://"/></td>
                </tr>
                <tr>
                    <td></td>
                    <td colspan="7"><input style="background-color: #00FF00;color: white" type="submit" value="確認提交"/></td>
                </tr>
            </table>
        </form>
    </body>
</html>
---------------------
作者:鄭清
來源:CSDN
原文:https://blog.csdn.net/qq_38225558/article/details/82700939
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
 
         
所涉及的python代碼

from
lxml import html def parse(): """"將html文件中的內容,使用小path進行提取""" #讀取文件中的內容 f = open('./venv/static_/index.html','r',encoding = 'utf-8') s = f.read() selector = html.fromstring(s) #j解析標題 h3 = selector.xpath('/html/body/h3/text()') print(h3[0])#這里取到的是個list,我用使用列表獲取 f.close() #解析ul里面的內容 ul = selector.xpath('/html/body/ul/li') # ul = selector.xpath('//ul/li')也可以使用 print(len(ul)) for li in ul: print(li.xpath('text()')[0]) #解析tr里面的內容 # tr = selector.xpath('/html/body/form/table/tr/td/text()') # print(tr) #解析ul指定的元素值 ul2 = selector.xpath('/html/body/ul/li[@class="important"]/text()') print(ul2) #解析ul指定的元素屬性 a = selector.xpath('//div[@id="container"]/a/text()') print(a[0]) #href屬性 alink = selector.xpath('//div[@id="container"]/a/@href') print(alink[0]) #解析p標簽 p = selector.xpath('/html/body/p/text()') # p = selector.xpath('/html/body/p[last()]/text()') #獲取最后一個 print(len(p)) print(p[0]) #使用瀏覽的xpath生成工具 test = selector.xpath('/html/body/form/table/tr[1]/th/text()')#只能借鑒/html/body/form/table/tbody/tr[1]/th print(test[0]) if __name__== '__main__': parse()



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM