日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python网络爬虫系列(五)——数据提取 jsonpath模块

發布時間:2024/7/5 python 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python网络爬虫系列(五)——数据提取 jsonpath模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、數據提取概述

知識點
  • 了解 響應內容的分類
  • 了解 xml和html的區別

1. 響應內容的分類

在發送請求獲取響應之后,可能存在多種不同類型的響應內容;而且很多時候,我們只需要響應內容中的一部分數據

  • 結構化的響應內容

    • json字符串

      • 可以使用re、json等模塊來提取特定數據
      • json字符串的例子如下圖

    • xml字符串

      • 可以使用re、lxml等模塊來提取特定數據

      • xml字符串的例子如下

        <bookstore> <book category="COOKING"><title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"><title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"><title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
  • 非結構化的響應內容

    • html字符串

      • 可以使用re、lxml等模塊來提取特定數據
      • html字符串的例子如下圖


知識點:了解 響應內容的分類

2. 認識xml以及和html的區別

要搞清楚html和xml的區別,首先需要我們來認識xml

2.1 認識xml

xml是一種可擴展標記語言,樣子和html很像,功能更專注于對傳輸和存儲數據

<bookstore> <book category="COOKING"><title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"><title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"><title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>

上面的xml內容可以表示為下面的樹結構:

2.2 xml和html的區別

二者區別如下圖

  • html:
    • 超文本標記語言
    • 為了更好的顯示數據,側重點是為了顯示
  • xml:
    • 可擴展標記語言
    • 為了傳輸和存儲數據,側重點是在于數據內容本身

知識點:了解 xml和html的區別

2.3 常用數據解析方法

據,側重點是為了顯示

  • xml:
    • 可擴展標記語言
    • 為了傳輸和存儲數據,側重點是在于數據內容本身

知識點:了解 xml和html的區別

2.3 常用數據解析方法

二、數據提取-jsonpath模塊

知識點
  • 了解 jsonpath模塊的使用場景
  • 掌握 jsonpath模塊的使用

1. jsonpath模塊的使用場景

如果有一個多層嵌套的復雜字典,想要根據key和下標來批量提取value,這是比較困難的。jsonpath模塊就能解決這個痛點,接下來我們就來學習jsonpath模塊

jsonpath可以按照key對python字典進行批量數據提取


知識點:了解 jsonpath模塊的使用場景

2. jsonpath模塊的使用方法

2.1 jsonpath模塊的安裝

jsonpath是第三方模塊,需要額外安裝

pip install jsonpath

2.2 jsonpath模塊提取數據的方法

from jsonpath import jsonpath ret = jsonpath(a, 'jsonpath語法規則字符串')

2.3 jsonpath語法規則

2.4 jsonpath使用示例

book_dict = { "store": {"book": [ { "category": "reference","author": "Nigel Rees","title": "Sayings of the Century","price": 8.95},{ "category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","price": 12.99},{ "category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99},{ "category": "fiction","author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99}],"bicycle": {"color": "red","price": 19.95}} }from jsonpath import jsonpathprint(jsonpath(book_dict, '$..author')) # 如果取不到將返回False # 返回列表,如果取不到將返回False

3. jsonpath練習

我們以拉勾網城市JSON文件 http://www.lagou.com/lbs/getAllCitySearchLabels.json 為例,獲取所有城市的名字的列表,并寫入文件。

參考代碼:

import requests import jsonpath import json# 獲取拉勾網城市json字符串 url = 'http://www.lagou.com/lbs/getAllCitySearchLabels.json' headers = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"} response =requests.get(url, headers=headers) html_str = response.content.decode()# 把json格式字符串轉換成python對象 jsonobj = json.loads(html_str)# 從根節點開始,獲取所有key為name的值 citylist = jsonpath.jsonpath(jsonobj,'$..name')# 寫入文件 with open('city_name.txt','w') as f:content = json.dumps(citylist, ensure_ascii=False)f.write(content)
知識點:掌握 jsonpath模塊的使用

ent = json.dumps(citylist, ensure_ascii=False)
f.write(content)

----##### 知識點:掌握 jsonpath模塊的使用----

總結

以上是生活随笔為你收集整理的python网络爬虫系列(五)——数据提取 jsonpath模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。