����λ�ã���ҳ > �����̳� > �̳� > Xpath���������﷨

Xpath���������﷨

��Դ������������|��ʱ�䣺2024-12-04 10:15:46 |���Ķ���185��|�� ��ǩ�� a T XP �� |����������

XPath���� XPath(XML Path Language)��һ��������XML��HTML�ĵ��в�����Ϣ������,��ͨ��·������ʽ����λ�ڵ�,���Ժ��ı�����,��֧�ָ��Ӳ�ѯ����,XPath ������ Web ץȡ������ Scrapy,Selenium �ȵĺ��ļ���֮һ XPath �����Ļ������� ����

XPath����

XPath(XML Path Language)��һ��������XML��HTML�ĵ��в�����Ϣ������,��ͨ��·������ʽ����λ�ڵ�,���Ժ��ı�����,��֧�ָ��Ӳ�ѯ����,XPath ������ Web ץȡ������ Scrapy,Selenium �ȵĺ��ļ���֮һ

XPath ������������

  1. ����lxml.etree

    from lxml import etree
    
  2. ʹ��etree.parse(filename, parser=None)��������һ�����νṹ

    • etree.parse() ���ڽ�������XML��HTML�ļ�,������ת��Ϊһ�����νṹ�� ElementTree ����,����ͨ���ö�������ĵ��ĸ����ڵ�
    • filename :Ҫ�������ļ�·��
    • parser (��ѡ):Ĭ�������,parser()������ļ���չ���Զ�ѡ����ʵĽ�����,�� .xml �ļ�ʹ��XML������,.htmlʹ��HTML������
  3. ʹ��etree.HTML(html_string, parser=None)��������html�ַ���

    • html_string :Ҫ������HTML�ַ���
    • parser :(��ѡ):Ĭ������� etree.HTML() ʹ�� etree.HTMLparser() �����
    • ����ֵ :etree.HTML()����һ�� ELement ����,��ʾHTML�ĵ��� ��Ԫ�� ,����ͨ���ö�������ĵ������ڵ�
  4. ʹ��.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�﷨

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�

ν���� 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,...] 

XPath,re����,BeautifulSoup�Ա�

��֮ǰ��ѧϰ����������ѧϰ��re�������ʽ,���ѧϰ�˸��ӱ�ݵ�bs4,��Ϊ�λ�ҪѧϰXPath������,���������ǽ����ǵ��ŵ�����ó������жԱ�ѧϰ

���� �ŵ� ȱ�� ���ó���
XPath ǿ���·������������֧�ֲ㼶�ṹ��������ѯ ѧϰ���߽϶����Բ��淶 HTML �ݴ��Խϲ� �ṹ�����õ� XML/HTML�����Ӳ�ѯ
re ����Ըߣ��ʺϴ������ı��е�ģʽƥ�� ���ʺϽ��� HTML/XML���ɶ��Բ� �Ӵ��ı�����ȡ�ض�ģʽ������
BeautifulSoup ����ʹ�ã��ݴ���ǿ���ʺϳ�ѧ�� �����Եͣ��������� ���淶�� HTML����������ȡ����ҳץȡ
  • �ܽ�
    • ����Ҫ�����ṹ���õ�XML��HTML�ĵ�,����Ҫ���и��Ӳ�ѯ ,��ôXPath���������ѡ��
    • ����Ҫ�Ӵ��ı�����ȡ�ض�ģʽ������ʱ ,�����־����ȡ����,IP��ַ��,re�������ʽ�����ѡ��
    • ��Ҫ�������淶�� HTML ����ֻ��Ҫ���мòµ¥µï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½È¡, BeautifulSoup �����Ѻõ�ѡ��
С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

a 1.0
a 1.0
���ͣ���������������Ӫ״̬����ʽ��Ӫ�������ԣ����� ����

��Ϸ����

��Ϸ���

��Ϸ��Ƶ

��Ϸ����

��Ϸ�

��alittletotheleft������������һ��ܻ�ӭ����������������Ϸ����ҵ������Ƕ��ճ������еĸ���������

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������