xpath总结

XPath(XML Path Language)是一门在XML文档中查找信息的语言;可用来在XML文档中对元素和属性进行遍历。

python中如何安装使用XPath:

①: 安装 lxml 库。

②: from lxml import etree

③: Selector = etree.HTML(网页源代码)

④: Selector.xpath(一段神奇的符号)

1.2.1. 准备工作:

要使用XPath首先要先安装lxml库:

pip install lxml

谷歌浏览器配置XPath插件

 

配置参考链接:谷歌浏览器插件xpath helper 的安装和使用

如果安装报 包装包无效,解决办法:xpath helper插件安装提示程序包无效

<!DOCTYPE html>
<html>
<head>
<title>我的网页</title>
</head>
<body>
<h3 id="hid">我的常用链接</h3>
<ul>
<li class="item-0"><a href="百度一下,你就知道">百度</a></li>
<li class="item-1 shop"><a href="京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!">京东</a></li>
<li class="item-2"><a href="搜狐">搜狐</a></li>
<li class="item-3"><a href="WWW.SINA.COM">新浪</a></li>
<li class="item-4 shop"><a href="http://www.taobao.com">淘宝</a></li>
</ul>
</body>
</html>

# 获取节点

         " //* ":# 获取网页中所有标签并遍历输出标签名

         " //li ": # 获取所有li节点

         " //li/a " :# 获取所有li节点下的所有直接a子节点

         " //ul//a ":# 效果同上(ul下所有子孙节点)

         " //a/..": #获取所有a节点的父节点

# 获取属性和文本内容

" //li/a/@href  ": #获取所有li下所有直接子a节点的href属性值  ---->"京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!"

" //li/a/text() ": #获取所有li下所有直接子a节点内的文本内容    ----->['百度', '京东', '搜狐', '新浪', '淘宝']

" //li/a[@class]/text() ": #获取所有li下所有直接含有class属性子a节点内的文本内容      ----->#['百度', '搜狐', '新浪']

" //li/a[@class='aa']/text() ":#获取所有li下所有直接含有class属性值为aa的子a节点内的文本内容  -----> #['搜狐', '新浪']

" //li[contains(@class,'shop')]/a/text() ":#获取class属性值中含有shop的li节点下所有直接a子节点内的文本内容 --->#['搜狐', '新浪']

 

re.findall 与 xpath 互转  实例

import re
import requests
from time import sleep

from lxml import etree
from selenium import webdriver
class llll():
    def one(self): #初始方法,自动执行的
        self.dr=webdriver.Chrome()
        self.dr.get('http://ahfy.chinacourt.gov.cn/index.shtml')
        # sleep(3)
        # self.dr.refresh()#刷新网页
        # sleep(2)
        self.dr.find_element_by_link_text('司法公开').click()
        # sleep(3)
        # self.dr.find_element_by_xpath('//*[@id="category"]/div[2]/a').click()
        # sleep(3)
        # self.dr.close()#关闭网页
    def sdfsdf(self):
        import requests
        import re
        # import pymysql
        # conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="mysql", db="jjj", charset="utf8")
        # cur = conn.cursor()
        url='http://ahfy.chinacourt.gov.cn/article/detail/2020/01/id/4754356.shtml'
        headers={
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) 37abc/2.0.6.16 Chrome/60.0.3112.113 Safari/537.36'
        }
        result=requests.get(url=url,headers=headers).text
        shuju=etree.HTML(result)
        title=shuju.xpath('//div[@class="b_title"]/text()')
        # # title=re.findall("<div class='b_title'>(.*?)</div>",result)[0]
        author_1=shuju.xpath('//div[@class="sth_a"]/span/text()')[0]
        true_author=author_1.strip()#去除字符串内空格
        import re
        # time=re.findall('\d+-\d+-\d+',true_author)[0]
        # time=re.findall('时间:(.*?) ',true_author)[0]#时间:(.*?)空格
        author=true_author[3:6]
        content=shuju.xpath('//p//span[@style="font-family:宋体"]/text()')
        print(content)
        # # author=(re.findall("(.*?)&nbsp;&nbsp;",result)[0]).replace('\t','')
        # # time=re.findall("时间:(\d+-\d+-\d+ \d+:\d+)",result)[0]
        # # content=re.findall('宋体(.*?)<',result)
        # # # print(title)
        # # gg=''
        # # for i in content:
        # #     gg+=i
        # # aa_content=gg.replace(';','').replace('"','').replace('>','').replace('&nbsp','')
        # # aa=[]
        # sql = """insert into fayuan (title,author,time,content) values ('%s','%s','%s','%s')""" % (title,author,time,aa_content)
        # cur.execute(sql)
        # conn.commit()
        # cur.close()
        # conn.close()
    def gy(self):
        url='http://ahfy.chinacourt.gov.cn/article/index/id/Myi2NDAwNjAwNCACAAA/page/1.shtml'
        headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
        }
        resullll=requests.get(url=url,headers=headers).text
        # title=re.findall("target='_blank' title='(.*?)'>",resullll)[20:]
        # time=re.findall("<span class='right'>(.*?)</span>",resullll)
        # aa=[]
        # for i in range(0,len(title)):
        #     aa.append({'title':title[i],'time':time[i]})
        # print(resullll)
        # print(aa)
        shuju=etree.HTML(resullll)
        # title=shuju.Xpath("//li//a[@title]")
        title=shuju.xpath('//div[@id="category"]//ul/li//a/@title')
        http=shuju.xpath('//div[@id="category"]//ul//li//span[@class="left"]/a/@href')
        time=shuju.xpath('//*[@class="right"]/text()')
        print(time)
        # print(http)

if __name__ == '__main__':
    aaa=llll()
    aaa.sdfsdf()

参考:https://zhuanlan.zhihu.com/p/90911772

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用BeautifulSoup库获取xpath的内容是不直接支持的,因为BeautifulSoup是一个用于解析HTML和XML文档的库,而xpath是一种用于在XML文档中定位元素的查询语言。然而,你可以使用BeautifulSoup和xpath结合使用来实现这个功能。 首先,你需要使用BeautifulSoup解析HTML或XML文档,得到一个BeautifulSoup对象。然后,你可以使用BeautifulSoup对象的find_all方法来查找符合特定条件的元素。在find_all方法中,你可以使用标签名、属性名和属性值等来指定要查找的元素。 例如,假设你有一个BeautifulSoup对象soup,你想要获取所有a标签的href属性值,你可以使用以下代码: ``` links = soup.find_all('a') for link in links: href = link.get('href') print(href) ``` 这样就可以获取到所有a标签的href属性值了。 需要注意的是,BeautifulSoup的find_all方法返回的是一个列表,你可以通过遍历列表来获取每个元素的属性值或文本内容。 希望这个回答对你有帮助!\[1\] \[2\] \[3\] #### 引用[.reference_title] - *1* [Python爬虫常用库requests、beautifulsoup、selenium、xpath总结](https://blog.csdn.net/qq_45176548/article/details/118187068)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [BeautifulSoup 与 Xpath](https://blog.csdn.net/kermitJam/article/details/82634865)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python爬虫之bs4解析和xpath解析](https://blog.csdn.net/m0_52656075/article/details/122896071)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值