# 安装 pip install requestXpath
# 导入
from requestXpath import requests
# 使用样例
response = requests.get(url='http://www.runoob.com/python3/python3-tutorial.html')
print(response) # 返回response对象
print(response.status_code) # 返回response状态码
# print(response.text) # 返回response源码
# print(response.json) # 返回response Json
item = {}
TreeItem = response.tree # 实例化tree
title = TreeItem.xpath("//div[@id='content']/h1") # xpath提取(默认'.//text()', 支持'/@href')
filter_title = TreeItem.xpath("//div[@id='content']/h1|//div[@id='content']/h2", filter="div[@id='content']/h2") # xpath提取, 过滤器, 开头不需要'//'
re_title = TreeItem.xpath("//div[@id='content']/h1", rule='Python 3(.*)') # xpath提取, 支持正则
titleXpathObj = TreeItem.xxpath("//div[@id='content']/h1/text()") # 原生xpath提取
link = TreeItem.xpath("//div[@id='leftcolumn']/a/@href", is_list=True) # xpath提取(is_list:True返回列表)
item['标题'], item['正则标题'], item['链接'], item['titleXpathObj'], item['filter_title'] = title, re_title, link, titleXpathObj, filter_title
print(item)
————————————————
版权声明:本文为CSDN博主「Dwyane Wade.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接: