python使用xpath获取页面元素的使用
作者:Sun@Python 时间:2021-04-30 10:32:09
关于python 使用xpath获取网页信息的方法?
1、xpath的使用方法?
XPath 使用路径表达式来选取 XML 文档中的节点或节点集。节点是通过沿着路径 (path) 或者步 (steps) 来选取的。
常用路径表达式含义
表达式 | 描述 |
---|---|
/ | 从根节点选取(取子节点) |
// | 选择的当前节点选择文档中的节点 |
. | 选取当前节点。 |
… | 选取当前节点的父节点。 |
@ | 选取属性 |
* | 表示任意内容(通配符) |
| | 运算符可以选取多个路径 |
常用功能函数
函数 | 用法 | 解释 |
---|---|---|
startswith() | xpath(‘//div[starts-with(@id,”ma”)]‘) | #选取id值以ma开头的div节点 |
contains() | xpath(‘//div[contains(@id,”ma”)]‘) | #选取id值包含ma的div节点 |
and() | xpath(‘//div[contains(@id,”ma”) and contains(@id,”in”)]‘) | #选取id值包含ma的div节点 |
text() | _.xpath('./div/div[4]/a/em/text()') | #选取em标签下文本内容 |
备注:
1、html中当相同层次存在多个标签例如div,它们的顺序是从1开始,不是0
2、浏览器中使用开发者工具可以快速获取节点信息
2、实例:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/9/7 9:35
# @Author : Sun
# @Email : 8009@163.com
# @File : sun_test.py
# @Software: PyCharm
import requests
from lxml import etree
def get_web_content():
try:
url = "htpps://***keyword=%E6%97%A0%E9%92%A2%E5%9C%88&wq=%E6%97%A0%E"
"9%92%A2%E5%9C%88&ev=1_68131%5E&pvid=afbf41410b164c1b91d"
"abdf18ae8ab5c&page=5&s=116&click=0 "
header = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64)"
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/75.0.3770.100 Safari/537.36 "}
response = requests.request(method="Get", url=url, headers=header)
result = response.text
return result
except TimeoutError as e:
return None
def parsing():
result = get_web_content()
if result is not None:
html = etree.HTML(result)
# 先获取一个大的节点,包含了想要获取的所有信息
ii = html.xpath('//*[@id="J_goodsList"]/ul/li')
for _ in ii:
# 采用循环,依次从大节点中获取小的节点内容
# ''.join() 将列表中的内容拼接成一个字符串
infoResult = {
# @href 表示:获取属性为href的内容
'href': "https:" + _.xpath('./div/div[1]/a/@href')[0],
'title': ''.join(
_.xpath('./div/div[2]/div/ul/li/a/@title')),
# text()表示获取节点i里面的文本信息
'price': _.xpath('./div/div[3]/strong/i/text()')[0],
'info': ''.join(
_.xpath('./div/div[4]/a/em/text()')).strip(),
'province': _.xpath('./div/div[9]/@data-province')[0]}
print(infoResult)
else:
raise Exception("Failed to get page information, please check!")
return None
if __name__ == '__main__':
parsing()
结果图片:
来源:https://blog.csdn.net/qq_40267002/article/details/120197707
标签:python,xpath,获取,元素
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 解决logging功能使用过程中遇到的一个问题
2023-05-25 11:31:05
java EJB 加密与解密原理的一个例子
2023-10-02 06:41:16
PyCharm2019 安装和配置教程详解附激活码
2021-12-10 02:07:38
![](https://img.aspxhome.com/file/2023/3/115103_0s.png)
Python数据结构之递归方法详解
2021-05-11 09:10:37
![](https://img.aspxhome.com/file/2023/3/123523_0s.png)
python使用os模块的os.walk遍历文件夹示例
2021-05-21 12:30:07
如何从SQL数据库中调用图片?
2009-11-15 19:59:00
asp如何做一个检索结果带链接的检索?
2010-07-11 21:15:00
python直接获取API传递回来的参数方法
2023-11-10 18:01:59
Asp用正则表达式获取文章中的所有图片地址
2010-07-17 13:11:00
解决pycharm下载库时出现Failed to install package的问题
2021-07-30 14:15:49
![](https://img.aspxhome.com/file/2023/0/72890_0s.png)
Scrapy爬虫框架集成selenium及全面详细讲解
2021-07-28 18:47:09
![](https://img.aspxhome.com/file/2023/2/95832_0s.jpg)
python实现最大优先队列
2022-12-24 00:48:13
Python实现企业微信机器人每天定时发消息实例
2023-09-04 22:08:40
![](https://img.aspxhome.com/file/2023/9/64709_0s.jpg)
matplotlib 对坐标的控制,加图例注释的操作
2021-06-12 16:28:46
![](https://img.aspxhome.com/file/2023/4/90404_0s.jpg)
python跳过第一行快速读取文件内容的实例
2022-05-12 12:58:56
python使用socket向客户端发送数据的方法
2023-08-13 00:16:51
python实现k-means聚类算法
2022-03-16 22:06:07
![](https://img.aspxhome.com/file/2023/4/120604_0s.jpg)
解决pycharm每次新建项目都要重新安装一些第三方库的问题
2023-02-22 23:08:22
![](https://img.aspxhome.com/file/2023/0/110230_0s.jpg)
python修改字典键(key)的方法
2023-04-16 20:26:57
![](https://img.aspxhome.com/file/2023/3/107663_0s.jpg)
javascript 通用滑动门tab类
2023-08-05 09:42:25