����λ�ã���ҳ > �����̳� > �̳� > Xpath���������﷨
XPath���� XPath(XML Path Language)��һ��������XML��HTML�ĵ��в�����Ϣ������,��ͨ��·������ʽ����λ�ڵ�,���Ժ��ı�����,��֧�ָ��Ӳ�ѯ����,XPath ������ Web ץȡ������ Scrapy,Selenium �ȵĺ��ļ���֮һ XPath �����Ļ������� ����
XPath(XML Path Language)��һ��������XML��HTML�ĵ��в�����Ϣ������,��ͨ��·������ʽ����λ�ڵ�,���Ժ��ı�����,��֧�ָ��Ӳ�ѯ����,XPath ������ Web ץȡ������
Scrapy,Selenium
�ȵĺ��ļ���֮һ
����lxml.etree
from lxml import etree
ʹ��etree.parse(filename, parser=None)��������һ�����νṹ
etree.parse()
���ڽ�������XML��HTML�ļ�,������ת��Ϊһ�����νṹ��
ElementTree
����,����ͨ���ö�������ĵ��ĸ����ڵ�
filename
:Ҫ�������ļ�·��
parser
(��ѡ):Ĭ�������,parser()������ļ���չ���Զ�ѡ����ʵĽ�����,��
.xml
�ļ�ʹ��XML������,.htmlʹ��HTML������
ʹ��etree.HTML(html_string, parser=None)��������html�ַ���
html_string
:Ҫ������HTML�ַ���
parser
:(��ѡ):Ĭ�������
etree.HTML()
ʹ��
etree.HTMLparser()
�����
ELement
����,��ʾHTML�ĵ���
��Ԫ��
,����ͨ���ö�������ĵ������ڵ�
ʹ��.xpath(xpath_expression)���Ѿ������õ�HTML�ĵ���ִ��XPath��ѯ
result = html_tree.xpath(xpath_expression)
xpath_expression
:XPath����ʽ,�������ĵ��в��ҽڵ�,XPath����ʽ�����Ǿ���·�������·��,Ҳ���԰���ν��,�����������,��Ҫ��XPath�﷨�����չ������
html_tree
:������
ElementTree
����(�� etree.parse() ����)��
Element
����(�� etree.HTML() ����)
from lxml import etree
# ʹ��etree.parser()�����ļ�·��
parser = etree.HTMLParser(encoding='utf-8') # ��utf8�����
tree = etree.parse('../Learning02/��������.html', parser=parser)
print(tree)
#output->
# ʹ��etree.HTML()���������ļ������綯̬HTML
# ��ȡ�ļ� ����Ϊ�ַ���
file = open('../Learning02/��������.html', 'r', encoding='utf-8')
data = file.read()
root = etree.HTML(data)
print(root)
#����
root = etree.HTML(open('../Learning02/��������.html', 'r', encoding='utf-8').read())
print(root)
#output->
XPath
�﷨����������XML��HTML�ĵ��в�����Ϣ������
XPathʹ��·������ʽ����λ�ĵ��еĽڵ�,·��Ҳ���Է�Ϊ����·�������·��
/
:��ʾ�Ӹ��ڵ㿪ʼѡ��,�����ڶ���һ������·��
�Ӹ��ڵ�html��ʼ���ҵ�head,�ٴ�head���ҳ�title��ǩ
root = etree.HTML(open('../Learning02/��������.html', 'r', encoding='utf-8').read())
all_titles = root.xpath('/html/head/title')
for title in all_titles:
print(etree.tostring(title, encoding='utf-8').decode('utf-8'))
#output-> ���������塷ȫ�������Ķ�_ʷ��伮_ʫ��������
��������·��,���·��ʹ���ʸ���,������
//
:��ʾ��
��ǰ�ڵ㿪ʼ,
ѡ���ĵ���
���з��������Ľڵ�,
���Ҳ��������ǵ�λ��
root = etree.HTML(open('../Learning02/��������.html', 'r', encoding='utf-8').read())
all_a = root.xpath('//a')
for a in all_a:
print(a.text)
#None
#��ҳ
#����
#����
#...
./
:��ʾ��ǰ�ڵ�,ͨ������ָ����ǰ�ڵ㱾��,�������
all_a = root.xpath('//a')
print(all_a[1].xpath('./text()')) #./��ʾ��ǰ��a��ǩ
#output-> ['��ҳ']
@
:����ѡ��Ԫ�ص�����,������Ԫ�ر���
# ʹ�� @ ѡ�� ��ǩ�� href ����
all_hrefs = root.xpath('//a[@href]')
for hrefs in all_hrefs:
print(etree.tostring(hrefs, encoding='unicode'))
���
xpath
�����ڽ�һ��ɸѡ�ڵ�ı���ʽ,ͨ�����ڷ�����[]
��,����Ի��ڽڵ��λ��,����ֵ,�ı����ݻ����������� ѡ���ض��Ľڵ�,ν�����Ƕ��ʹ��,Ҳ����������ν�����ʹ��
�����﷨
//element[condition]
element
:Ҫѡ���Ԫ��
condition
:ν���е�����,���ڽ�һ��ɸѡ����������Ԫ��
λ��ν�����ڸ��ݽڵ����ֵܽڵ��е�λ�ý���ѡ��,����ʹ��
position()
��ֱ��ָ��λ�ñ��
��ȡ��һ��
ul
��ǩ�еĵ�һ��
li
��ǩ
#//ul��ȡ��������ul,[0]ѡ���һ��
lis = root.xpath('//ul')[0].xpath('./li[1]')
for li in lis:
print(etree.tostring(li, encoding='unicode'))
#output-> - ��ҳ
ʹ��
last()
��ȡ����һ���ڵ�,�͵����ڶ����ڵ�
# ��һ��
last_li = root.xpath('//ul')[0].xpath('./li[last()]')
print(etree.tostring(last_li[0], encoding='unicode'))
# ������
last_second_li = root.xpath('//ul')[0].xpath('./li[last()-1]')
print(etree.tostring(last_second_li[0], encoding='unicode'))
#output-> - ��׿����
#- �ż�
ʹ��
position()
��ȡλ�ý���ɸѡ
# ��ȡǰ����li��ǩ
last_li = root.xpath('//ul')[0].xpath('./li[position()<3]')
for li in last_li:
print(etree.tostring(li, encoding='unicode'))
# ��ȡż��λ��ǩ
lis = root.xpath('//ul')[0].xpath('./li[position() mod 2=0]')
for li in lis:
print(etree.tostring(li, encoding='unicode'))
�����
����ν������ ѡ������ض����ԵĽڵ�
@attribute
����ȡ��������,�����������ɸѡ
# ѡȡ���о��� href ���Ե� a Ԫ��
hrefs = root.xpath("//a[@href]")
for href in hrefs:
print(etree.tostring(href, encoding='unicode'))
class
����ֵ
all_class = root.xpath('//@class')
print(all_class)
����
��������������һ��,ʹ���߼������
and,or
�������������ӵ�ν��
#ѡȡhref����ֵΪhttps://example.com��class����ֵΪlink��aԪ��
//a[@href='https://example.com' and @class='link']
#ѡȡhref����ֵΪhttps://example.com��https://another.com��a Ԫ��
//a[@href='https://example.com' or @href='https://another.com']
�����
Xpath�ṩ���������ú���,��Ӧ�Ը����ӵ�ɸѡ����
contains((string1, string2)
����:
string1
:Ҫ�������ַ���
string2
:Ҫ���ҵ��ַ���
# ѡȡclass����"book"��img��ǩ
images = root.xpath('//img[contains(@src,"book")]')
for image in images:
print(etree.tostring(image, encoding='unicode'))
starts-with(string1, string2)
����:
���һ���ַ����Ƿ���ָ���ַ���ǰ׺��ʼ,�Ƿ���
true
,�񷵻�false
string1:
Ҫ�����ַ���
string2:
��Ϊǰ׺���ַ���
# ѡȡ����href��https://��ͷ��a��ǩ
all_a = root.xpath('//a[starts-with(@href,"https:")]')
for a in all_a:
print(etree.tostring(a, encoding='unicode'))
�ı�����ν��
����ѡ������ض��ı����ݵĽڵ�,����ʹ��
text()
��������ȡ�ڵ���ı�����
# ѡ��ʹ�ð���"����"�ı���p��ǩ
paragraphs = root.xpath('//p[contains(text(),"����")]')
for p in paragraphs:
print(etree.tostring(p, encoding='unicode'))
xpath�ṩ�˶���ͨ���,������·������ʽ��ƥ��δ֪��Ԫ��,����,���κνڵ�.��Щͨ����dz�����,�����ǵ���ȷ������ڵ����ƺͽṹ�������
ͨ��� | ���� |
---|---|
* | ƥ���κ�Ԫ�ؽڵ㡣 һ�����������copy xpath����� |
@* | ƥ���κ����Խڵ㡣 |
node() | ƥ���κ����͵Ľڵ㡣 |
*
ƥ���κ�Ԫ�ؽڵ�
*
����õ�ͨ���֮һ,�����ƥ���κ�Ԫ��,������Ҫ�����ǩ��.���ڲ�ȷ��Ԫ�����ƻ�ϣ��ѡ���������͵�Ԫ��ʱ�dz�����
# ѡ������ div �µ�������Ԫ��
divs = root.xpath("//div/*")
for div in divs:
print(etree.tostring(div, encoding='unicode'))
@*
ƥ���κ����Խڵ�
@*
����ƥ���κ����Խڵ�,������ָ��������������,���㲻ȷ���������ƻ�ϣ��ѡ����������ʱ�dz�����
# ѡ������ a Ԫ�ص���������
all_a = root.xpath('//a/@*')
for a in all_a:
print(a)
node()
ƥ���κ����͵Ľڵ�
node()
��һ����ͨ�õ�ͨ���,����ƥ���κ����ͽڵ�,����Ԫ�ؽڵ�,�ı��ڵ�,���Խڵ�,ע�ͽڵ�ȵ�,������Ҫѡ�񲻽�����Ԫ�ؽڵ���ʮ������
# ѡ������ ul �µ������ӽڵ�(�����ı��ڵ�)
nodes = root.xpath('//ul/node()')
print(nodes)
#output-> ['\n ', , '\n,...]
��֮ǰ��ѧϰ����������ѧϰ��re�������ʽ,���ѧϰ�˸��ӱ�ݵ�bs4,��Ϊ�λ�ҪѧϰXPath������,���������ǽ����ǵ��ŵ�����ó������жԱ�ѧϰ
���� | �ŵ� | ȱ�� | ���ó��� |
---|---|---|---|
XPath
|
ǿ���·������������֧�ֲ㼶�ṹ��������ѯ | ѧϰ���߽϶����Բ��淶 HTML �ݴ��Խϲ� | �ṹ�����õ� XML/HTML�����Ӳ�ѯ |
re
|
����Ըߣ��ʺϴ������ı��е�ģʽƥ�� | ���ʺϽ��� HTML/XML���ɶ��Բ� | �Ӵ��ı�����ȡ�ض�ģʽ������ |
BeautifulSoup
|
����ʹ�ã��ݴ���ǿ���ʺϳ�ѧ�� | �����Եͣ��������� | ���淶�� HTML����������ȡ����ҳץȡ |
ʹ��Blender���ɳ���ģ��
�Ķ�ȫ����������ERA5�����ط���
�Ķ�Xpath���������﷨
�Ķ�����ѧϰ�������繹�����£�
�Ķ���ΪMateƷ��ʢ�䣺HarmonyOS NEXT�ӳ�����Ϸ���ܵõ�����ͷ�
�Ķ�ʵ�ֶ��󼯺���DataTable���໥ת��
�Ķ�Ӳ�̵Ļ���֪ʶ��ѡ��ָ��
�Ķ�������й��ƶ��ı�ͼ��ײ�
�Ķ�����NEXTԪ�����������ѿ����ϼ���Ʒ
�Ķ��ᳲ���С������������Ƽ��رշ���
�Ķ������ArcMap�����н���դ��ͼ���ز�������
�Ķ��㷨�����ݽṹ 1 - ģ��
�Ķ���Ѷ�����߿ͷ���Ӫ��ϵͳ����
�Ķ���Ѷ��Ƶҹ��ģʽ���ý̳�
�Ķ����ں���NEXT��Ѫ���Ŵ���������������
�Ķ�5. Spring Cloud OpenFeign ����ʽ WebService �ͻ��˵ij���ϸʹ��
�Ķ�Java����ģʽ����̬�����Ͷ�̬�����ĶԱȷ���
�Ķ�Win11�ʼDZ����Զ�����Ӧ�õ���ɫ����ʾ����
�Ķ�˼�� V1.5.6 ��׿��
��ս�귨 V7.5.0 ��׿��
У��������������׵������� V1.0 ��׿��
��˸֮�� V1.9.7 ��׿��
������Ե����� v1.0.4 ��׿��
������֮ŠV5.2.3 ��׿��
��������������Դ V1.0 ��׿��
���֮Ϣ V1.0 ��׿��
��ħ������������䣩 V1.0 ��׿��
���ں�������ϵ�����������������վ�����������������Ƽ�����
Ƶ�� ����Ƶ��������ר������������׿�������app����
�Ƽ� ��Ô���������°��������ܿ������ز���
���� ����ɫ������������ ���������ս������������
ɨ��ά�����������ֻ��汾��
ɨ��ά����������΢�Ź��ںţ�
��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]
��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������