Objective
Some post request parameters are in JSON format, mentioned in the previous second POST request, need to import JSON module processing.
Common interface return data is also JSON format, when we make judgments, often only need to extract a few of the key parameters on the line, this time we need JSON to parse the returned data.
I. Introduction to the JSON module
1.Json Introduction: Json, full name JavaScript Object Notation, is a lightweight data interchange format, commonly used in HTTP requests
2. You can use Help (json) to view the corresponding source code comment content
Encoding basic Python Object hierarchies::
>>> Import JSON
>>> json.dumps ([' foo ', {' Bar ': (' Baz ', None, 1.0, 2)}])
' [' foo ', {"bar": ["Baz", NULL, 1.0, 2]}] '
>>> print json.dumps ("\" Foo\bar ")
"\" Foo\bar "
>>> Print json.dumps (U ' \u1234 ')
"\u1234"
>>> print json.dumps (' \ \ ')
"\\"
>>> Print json.dumps ({"C": 0, "B": 0, "a": 0}, Sort_keys=true)
{"A": 0, "B": 0, "C": 0}
>>> from Stringio import Stringio
>>> io = Stringio ()
>>> json.dump ([' Streaming API '], io)
>>> Io.getvalue ()
' ["Streaming API] '
Second, Encode (Python->json)
1. First of all, why should encode,python inside bool value is true and False,json inside bool value is true and false, and case-sensitive, this is embarrassing, obviously is bool value.
The code written in Python, uploaded to JSON, is certainly not recognizable, so Python's code needs to be encode to become a JSON-recognizable data type.
2. For a simple example, the Dict type is JSON. dumps() turned Str,true into a true,false and became a fasle.
3. The following table of correspondence is crawled out of the source code of the JSON module. Python data class, after encode into JSON data type, the corresponding table is as follows
| | Python | JSON |
| +===================+===============+
| | Dict | Object |
| +-------------- -----+---------------+
| | List, tuple | Array |
| +-------------------+---------------+
| | STR, Unicode | string |
| +-------------------+---------------+
| | int, long, float | number |
| +-------------------+---------------+
| | True | true |
| +-------------------+---------------+
| | False | False |
| +-------------------+---------------+
| | None | null |
| +-------------------+---------------+
SAN, decode (Json->python)
1. With the third entry successful result: {"Success": true} as an example, what we really want to know is whether success this field returns TRUE or False
2. If output is in content byte, a string is returned: {"Success": true}, so it is inconvenient to get the result later.
3. If the JSON is decoded, the return is a dictionary: {u ' success ': True}, so as to obtain the following result, the dictionary to use the way to value: result2["Success"]
4. The same JSON data is converted into Python-identifiable data, and the corresponding table relationships are as follows
| +---------------+-------------------+
| | JSON | Python |
| +===============+===================+
| | Object | Dict |
| +---------------+-------------------+
| | Array | List |
| +---------------+-------------------+
| | string | Unicode |
| +---------------+-------------------+
| | Number (int) | int, Long |
| +---------------+-------------------+
| | Number (real) | float |
| +---------------+-------------------+
| | true | True |
| +---------------+-------------------+
| | False | False |
| +---------------+-------------------+
| | null | None |
| +---------------+-------------------+
Four, case analysis
1. For example, open the Express network: http://www.kuaidi.com/, search for a number, to determine whether its status has been signed
2. The implementation code is as follows
Five, reference code:
# Coding:utf-8
Import requests
url = "Http://www.kuaidi.com/index-ajaxselectcourierinfo-1202247993797-yunda.html"
headers = {
"User-agent": "mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0 "
} # Get method add a ser-agent to it.
s = requests.session ()
r = S.get (URL, headers=headers,verify=false)
result = R.json ()
data = result["Data" # Get content inside data
Print data
Print Data[0] # get the top of the data.
Get_result = data[0][' context '] # get signed status
Print Get_result
if u "sign" in Get_result:
Print "Delivery slip signed for success"
Else
Print "Not signed"
Interested in Python interface automation, you can add Python interface Automation QQ Group: 226296743
You can also pay attention to my personal public number:
Python interface automation 5-json data processing