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

python 使用xpath获取网页标签内容,第1张

获取指定html的标签内容


打开网页的开发者模式,得到路径标签,然后加上/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)

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/714112.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-24
下一篇2022-04-24

发表评论

登录后才能评论

评论列表(0条)

    保存