xpath的使用

首先下载lxml.   pip install lxml

>>> def getxpath(html):           #返回html的xml结构
    return etree.HTML(html)
>>> sample="""<html>
  <head>
    <title>My page</title>
  </head>
  <body>
    <h2>Welcome to my <a href="#" src="x">page</a></h2>
    <p>This is the first paragraph.</p>
    <!-- this is the end -->
  </body>
</html>
"""
>>> s1=getxpath(sample)
>>> s1.xpath('//title/text()')          #根据绝对路径取出内容
['My page']
>>> s1.xpath('/html/head/title/text()')         #相对路径取出内容
['My page']
>>> s1.xpath('//h2/a/@src')          #获取属性src
['x']
>>> s1.xpath('//@href')             #获取属性href
['#']
>>> s1.xpath('//text()')             #取出所有文本内容
['\n  ', '\n    ', 'My page', '\n  ', '\n  ', '\n    ', 'Welcome to my ', 'page', '\n    ', 'This is the first paragraph.', '\n    ', '\n  ', '\n']
>>> s1.xpath('//comment()')            #获取注释
[<!-- this is the end -->]

获取文本内容用text(),注释用comment(),其他的用@就好了。

>>> sample2="""
<html>
<body>
<ul>
<li>Quote 1</li>
<li>Quote 2 with <a href="...">link</a></li>
<li>Quote 3 with <a href="...">another link</a></li>
<li><h2>Quote 4 title</h2> ... </li>
</ul>
</body>
</html>
"""
>>> s2=getxpath(sample2)      
>>> s2.xpath('//li/text()')           #获取li标签下的内容
['Quote 1', 'Quote 2 with ', 'Quote 3 with ', ' ... ']
>>> s2.xpath('//li[position()=1]/text()')         #第一个li的内容
['Quote 1']
>>> s2.xpath('//li[1]/text()')          #两种获取方式
['Quote 1']
>>> s2.xpath('//li[position()=2]/text()')
['Quote 2 with ']
>>> s2.xpath('//li[position() mod2=1]/text()')   #获取所有奇数位li标签的内容
['Quote 1', 'Quote 3 with ']
>>> s2.xpath('//li[position() mod2=0]/text()')    #偶数位li
['Quote 2 with ', ' ... ']
>>> s2.xpath('//li[-1]/text()')   #这个是错误的
[]
>>> s2.xpath('//li[last()]/text()')    #这个才是正确的获取最后一个li内容
[' ... ']
>>> s2.xpath('//li[a]/text()')         #获取Li下面还有a的部分的内容
['Quote 2 with ', 'Quote 3 with ']
>>> s2.xpath('//li[a or h2]/text()')    #获取li下面有a或者h2的内容
['Quote 2 with ', 'Quote 3 with ', ' ... ']
>>> s2.xpath('//a/text()|//h2/text()')     #获取所有a和h2的内容
['link', 'another link', 'Quote 4 title']

位置第一个是1.最后一个要用last(),[-1]是错误的。

>>> sample3 = """<html>
  <body>
    <ul>
      <li id="begin"><a href="https://scrapy.org">Scrapy</a>begin</li>
      <li><a href="https://scrapinghub.com">Scrapinghub</a></li>
      <li><a href="https://blog.scrapinghub.com">Scrapinghub Blog</a></li>
      <li id="end"><a href="http://quotes.toscrape.com">Quotes To Scrape</a>end</li>
      <li data-xxxx="end" abc="abc"><a href="http://quotes.toscrape.com">Quotes To Scrape</a>end</li>
    </ul>
  </body>
</html>
"""
>>> s3=getxpath(sample3)
>>> s3.xpath('//li/a[@href="https://scrapy.org"]/text()')   
['Scrapy']
>>> s3.xpath('//li[@id="begin"]/text()')
['begin']
>>> s3.xpath('//li/a[text()="Scrapinghub"]/text()')
['Scrapinghub']
>>> s3.xpath('//li[@data-xxxx="end"]/text()')   #可以获取这个属性在那个标签下
['end']
>>> s3.xpath('//li[@abc="abc"]/text()')
['end']

可以根据属性或者文本直接定位到当前标签。

>>> sample4 = u"""
<html>
  <head>
    <title>My page</title>
  </head>
  <body>
    <h2>Welcome to my <a href="#" src="x">page</a></h2>
    <p>This is the first paragraph.</p>
    <p class="test">
    编程语言<a href="#">python</a>
    <img src="#" alt="test"/>javascript
    <a href="#"><strong>C#</strong>JAVA</a>
    </p>
    <p class="content-a">a</p>
    <p class="content-b">b</p>
    <p class="content-c">c</p>
    <p class="content-d">d</p>
    <p class="econtent-e">e</p>
    <p class="heh">f</p>
    <!-- this is the end -->
  </body>
</html>
"""
>>> s4=etree.HTML(sample4)
>>> s4.xpath('//p/text()')
['This is the first paragraph.', '\n    编程语言', '\n    ', 'javascript\n    ', '\n    ', 'a', 'b', 'c', 'd', 'e', 'f']
>>> s4.xpath('string(//p[@class="test"])').strip()           #获取p标签下的所有文本
'编程语言python\n    javascript\n    C#JAVA'
>>> s4.xpath('//p[starts-with(@class,"content")]/text()')     获取p标签下class有content的文本
['a', 'b', 'c', 'd']
>>> s4.xpath(('//p[contains(@class,"content")]/text()'))     #获取p标签下有class为content的文本
['a', 'b', 'c', 'd', 'e']


想要获取某个标签下所有的文本,要使用string。

starts-with是匹配字符串前面相等

contains是匹配任何位置相等都可以

最后的contains中的(@class,"content")可以根据需求改变,比如(text(),"content")或者(@src,"content")

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值