json parsing module

json.loads(json)

Json string data type formatted into Python

html_json = json.loads(res.text)

json.dumps(python)

Type into the python json type

import json
​
# json.dumps()之前
item = {'name':'QQ','app_id':1}
print('before dumps',type(item))
# json.dumps之后
item = json.dumps(item)
print('after dumps',type(item))

json.load(f)

The json file to read, and converted to type python

import json
​
with open('D:\\spider_test\\xiaomi.json','r') as f:
    data = json.load(f)
​
print(data)

json.dump(python,f,ensure_ascii=False)

Use the python json format data type into a string, generally let you save the captured data as json file

parameter

  • python: python types of data (dictionaries, lists, etc.)
  • f: file object
  • ensure_ascii: ensure_ascii = False # when coding sequence of
Import JSON 

# exemplary. 1 
Item = { ' name ' : ' Lion King ' , ' Card ' : ' Tulong ' } 
with Open ( ' yt.json ' , ' A ' ) AS F: 
    The json.dump (Item, F, ensure_ascii = False) 

# example 2 
item_list = [ 
    { ' name ' : ' paclitaxel Dragon King ' , ' Card ' : '123'},
    {'name': '青翼蝠王', 'card': '456'}
]
with open('ystlj.json', 'a') as f:
    json.dump(item_list, f, ensure_ascii=False)

Exercise: Tencent recruitment into the data to json file

# 1. __init__()
    self.f = open('tencent.json','a')
    self.item_list = []
# 2. parse_page()
    self.item_list.append(item)
# 3. main()
    json.dump(self.item_list,self.f,ensure_ascii=False)
    self.f.close()

json module summary

Reptile use up to json

1, crawling - json.loads (html)

  The contents of the response by the: json into python

2, data storage - json.dump (item_list, f, ensure_ascii = False)

  Save the captured data to a local json file

General data fetch processing mode

  1. txt file
  2. csv file
  3. json file
  4. MySQL database
  5. MongoDB database
  6. Redis database

 

Guess you like

Origin www.cnblogs.com/LXP-Never/p/11385892.html