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