xpath使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul class="clearfix">
    <li class="pa-3rXKoIIo"><a href="//finance.ifeng.com/" target="_blank" rel="nofollow me noopener noreferrer"
                               title="财经首页">财经首页</a></li>
    <li class=""><a href="//finance.ifeng.com/stock/" target="_blank" rel="nofollow me noopener noreferrer" title="股票">股票</a>
    </li>
    <li class=""><a href="//finance.ifeng.com/gold/" target="_blank" rel="nofollow me noopener noreferrer"
                    title="iMarkets">iMarkets</a></li>
    <li class=""><a href="//finance.ifeng.com/shanklist/1-66-" target="_blank" rel="nofollow me noopener noreferrer"
                    title="全球快报">全球快报</a></li>
    <li class=""><a href="http://finance.ifeng.com/hk" target="_blank" rel="nofollow me noopener noreferrer" title="港股">港股</a>
    </li>
    <li class=""><a href="//tech.ifeng.com/" target="_blank" rel="nofollow me noopener noreferrer" title="科技">科技</a>
    </li>
    <li class=""><a href="//finance.ifeng.com/money/" target="_blank" rel="nofollow me noopener noreferrer" title="理财">理财</a>
    </li>
    <li class=""><a href="//toujiao.ifeng.com/" target="_blank" rel="nofollow me noopener noreferrer" title="投教">投教</a>
    </li>
    <li class=""><a href="//ds.ifeng.com/" target="_blank" rel="nofollow me noopener noreferrer" title="大赛">大赛</a></li>
    <li class=""><a href="https://finance.ifeng.com/shanklist/1-74-" target="_blank"
                    rel="nofollow me noopener noreferrer" title="WEMONEY">WEMONEY</a></li>
    <li class=""><a href="//www.fengjr.com/cn/?c=1coa7kli3-1&amp;channel=1coa7kli3-1" target="_blank"
                    rel="nofollow me noopener noreferrer" title="凤凰金融">凤凰金融</a></li>
    <li class=""><a href="https://finance.ifeng.com/stock/special/sgkmh/" target="_blank"
                    rel="nofollow me noopener noreferrer" title="神光">神光</a></li>
    <li class=""><a href="http://culture.ifeng.com/huodong/special/2018sdzhjt/" target="_blank"
                    rel="nofollow me noopener noreferrer" title="舍得讲堂">舍得讲堂</a></li>
    <li class=""><a href="//finance.ifeng.com/institute/index.shtml" target="_blank"
                    rel="nofollow me noopener noreferrer" title="研究院"><h3>研究院</h3></a></li>
</ul>

</body>
</html>
例子

//ul[@class="clearfix"]/li 获取
class="clearfix"的url下的所有的li
//ul[@class="clearfix"]/li/@class 获取class="clearfix"的url下的所有的li的class属性
//ul[@class="clearfix"]/li/a/text()  获取class="clearfix"的url下的所有的li下的a标签的内容
//ul[@class="clearfix"]/li/a/@href  获取class="clearfix"的url下的所有的li下的a标签href的属性
//ul[@class="clearfix"]/li/a/@title  获取class="clearfix"的url下的所有的li下的a标签title的属性
 //ul/li/a/h3    获取的值为研究院  获取h3 要一级一级的写,漏掉会报错
//ul//h3
获取的值为研究院 //当前ul下的任意一个节点选取,不用一级一级的写

//a[@class="n"]/@href 获取下一页网址
//a[text()="下一页>"] 根据文本定位

 
 
<html><body>
<div>
 <div>
 
    <ul>
         <li class="item-0"><a href="link1.html">first item</a></li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-inactive"><a href="link3.html"><span class="bold">third item</span></a></li>
         <li class="item-1"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a></li>
     </ul>
 </div>
 </div>
</body></html>

/     从根节点选取。
//     从当前html中的任意位置开始选择。
.     选取当前节点。
..     选取当前节点的父节点。
@     选取属性。
li//a/text()获取a标签文本
li//a//text()获取a标签下所有文本 li
//a 表示li下的任何一个标签 li//span 1. 获取所有的 <li> 标签 result = html.xpath('//li') 2. 继续获取<li> 标签的所有 class属性 result = html.xpath('//li/@class') 3. 继续获取<li>标签下hre 为 link1.html 的 <a> 标签 result = html.xpath('//li/a[@href="link1.html"]') 4. 获取<li> 标签下的所有 <span> 标签 result = html.xpath('//li//span') 5. 获取 <li> 标签下的<a>标签里的所有 class result = html.xpath('//li/a//@class') 6. 获取最后一个 <li> 的 <a> 的 href result = html.xpath('//li[last()]/a/@href') 7. 获取倒数第二个元素的内容 result = html.xpath('//li[last()-1]/a/text()')
<div class="indent">


    <div class="">
        <p class="ul first"></p>
        <table width="100%" class=" xh-highlight">
            <tbody>
            <tr class="item">
                <td width="100" valign="top">


                    <a class="nbg" href="https://movie.douban.com/subject/30414462/" title="黑镜:潘达斯奈基">
                        <img src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2543862640.webp"
                             width="75" alt="黑镜:潘达斯奈基" class="">
                    </a>
                </td>

                <td valign="top">


                    <div class="pl2">

                        <a href="https://movie.douban.com/subject/30414462/" class="">
                            黑镜:潘达斯奈基
                            / <span style="font-size:13px;">黑镜:2018圣诞特别篇</span>
                        </a>


                        <p class="pl">2018-12-28(美国) / 菲恩·怀特海德 / 克雷格·帕金森 / 爱丽丝·洛维 / 阿西姆·乔杜里 / 威尔·保尔特 / 塔露拉·哈登 / 卡特里奥·诺克斯
                            / 保罗·布拉德利 / 乔纳森·阿里斯 / A·J·霍顿 / 弗勒尔·基思 / 劳拉·埃弗兰 / 阿伦·阿萨德 / 苏珊妮·伯登...</p>


                        <div class="star clearfix">
                            <span class="allstar35"></span>
                            <span class="rating_nums">7.0</span>
                            <span class="pl">(35794人评价)</span>

                        </div>


                    </div>

                </td>
            </tr>
            </tbody>
        </table>
        <div id="collect_form_30414462" class=""></div>
        <p class="ul"></p>
        <table></table>
        <table></table>
        <table></table>
       

    </div>


</div>

//div[@class="indent"]/div/table 获取所有table,一级一级选
//div[@class="indent"]//table 获取所有table
//div[@class="indent"]//table[1] 获取第一个table
//div[@class="indent"]//table//div[@class="pl2"]/a//text() 获取所有a下面的所有文本
//div[@class="indent"]//table//div[@class="pl2"]/a/@href 获取a标签的href
//div[@class="indent"]//table//a[@class="nbg"]/img/@src 所有图片的src

原文地址:https://www.cnblogs.com/chvv/p/10298504.html