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)

posted @ 2022-04-18 19:30  darling331  阅读(1010)  评论(0编辑  收藏  举报