python 使用xpath获取网页标签内容


获取指定html的标签内容

image
image
打开网页的开发者模式,得到路径标签,然后加上/text() 即可得到标签的文本内容
//*[@id="sonsyuanwen"]/div[1]/h1

对于网页爬取来说,还是很方便的

# -*- ecoding: utf-8 -*-
# @ModuleName: test005
# @Function: 
# @Author: darling
# @Time: 2022-04-18 13:58

import requests

from lxml import etree


def get_url():
    resource = requests.get('https://so.gushiwen.cn/shiwenv_444df93c9bdf.aspx')
    html = etree.HTML(resource.text)
    title = html.xpath('//*[@id="sonsyuanwen"]/div[1]/h1/text()')
    neir=html.xpath('//*[@id="contson444df93c9bdf"]/text()')
    print(title,neir)
    return resource


if __name__ == "__main__":
    res = get_url()
    print(res)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM