獲取指定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)