Introduction to the

Some post request parameters are in JSON format, which is mentioned in the previous post request and needs to be imported into THE JSON module for processing. Json data is easy to process, so most of the returned data is also in JSON format. When making judgment, we usually only need to extract it

A few key parameters will do, and this is where we need JSON to parse the returned data. In Python, bool values are True and False, and in JSON, bool values are True and False, and they’re case sensitive, which is embarrassing because they’re both bool values.

It is needless to say that the code written in Python cannot be recognized when it is transmitted to JSON. Therefore, python code needs to be encoded into the data type that can be recognized by JSON, whereas JSON data needs to be decode into the data type that can be recognized by Python code. This is also a need for beginners to pay attention to, is also a subtle sum

A pothole, it’s easy to fall into if you’re not careful. The author fell into, fortunately self-rescue ability is strong, climb out, so in order to alert later, there is this essay.

Introduction to JSON module

1. Introduction to Json: Json (full name JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition – December 1999). JSON is completely independent

Text format in the language, but also uses conventions similar to the C language family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language. JSON is easy for humans to read and write, but also easy for machines to parse and generate. Commonly used in HTTP requests, interfaces

In the data returned.

2, you can use help(json) to view the corresponding source comment content

Encode (python->json)

1, why encode, the author at the beginning of the small partners to come straight out, let you with questions to explore, to learn, to think

As a simple example, dict becomes STR after json.dumps (), True becomes True, and False becomes fasle

3. It can be seen from the corresponding source code of json module that Python data is converted into data identifiable by JSON, and the corresponding table relationship is as follows

Decoding the decode (json – > python)

1. Take the successful login result of Fiddler: {“success”:True} as an example, what we really want to know is whether success returns True or False so that we can assert the interface. The following is the successful login result of Fiddler

{“success”:true} is a string that returns a content byte

{u’success’: True} return a dictionary: {u’success’: True} return a dictionary: {u’success’: True} return a dictionary: {u’success’: True} return a dictionary: {u’success’: True} return a dictionary: {u’success’: True} return a dictionary: {u’success’: True

4, due to the change of the login mechanism of the blog park, we are following the return result of the last article to delete the essay, for friends to practice

5. Use Fiddler to catch the request to delete the newly created essay. As can be seen from the result of packet capture, the returned result is a string: {“isSuccess”:True}

6, code and result (see, it is so easy to get the value, then we can assert)

7. As can be seen from the corresponding source code of json module, json data is converted into data that can be recognized by Python, and the corresponding table relationship is as follows

8. Reference code

1 # coding: UTF-8 2 import requests 3 # Get some cookies 4 5 headers = {url = "https://passport.cnblogs.com/user/signin" 6 "the user-agent" : "Mozilla / 5.0 (Windows NT 10.0; WOW64; R = s.gate. r = s.gate. r = s.gate. r = s.gate. r = s.gate. r = s.gate. r = s.gate. r = s.gate. r = s.gate. r = s.gate. r = s.gate. r = s.gate. r Headers = headers, verify = False) 10 print (s.c ookies) 11 # 12 c = add login need two cookies requests. Cookies, RequestsCookieJar (13) c.set('.CNBlogsCookie', Complete above caught 'XXX') # 14 c.s. et ('. Cnblogs. AspNetCore. Cookies', 'XXX') # fill in c.s. et caught content above 15 (16 'AlwaysCreateItemsAsActive', "True")  c.set('AdminCookieAlwaysExpandAdvanced',"True") 17 s.cookies.update(c) 18 print (s.cookies) 19 result = r.content 20 Print (result. Decode (' utf-8)) # 21 after the success of the login to save edit content 22 23 body = url2 = "https://i.cnblogs.com/EditPosts.aspx?opt=1" {"__VIEWSTATE": "", 24 "__VIEWSTATEGENERATOR":"FE27D343", 25 "Editor$Edit$txbTitle":" This is bypassing the login title: Editor$Edit$EditorBody":"<p> http://www.cnblogs.com/duhong/</p>", 27 "Editor$Edit$Advanced$ckbPublished":"on", 28 "Editor$Edit$Advanced$chkDisplayHomePage":"on", 29 "Editor$Edit$Advanced$chkComments":"on", 30 "Editor$Edit$Advanced$chkMainSyndication":"on", 32 "Editor$Edit$lkbDraft":" Save as draft ", 32} 33 r2 = s.post(url2, Data =body, verify=False) print (r.tent. decode('utf-8')) 37 import re 38 postid= re.findall(r"postid=(.+?)&", Print (postid[0]) print(postid[0]) print(postid[0]) Delete the draft box 44 url3 = "https://i.cnblogs.com/post/delete" 45 json3 = {" postId ": postid[0]} 46 r3 = s.post(url3, json=json3, 48 print(type(result)) 49 print(result) 50 52 print(type(result1)) 53 print(result1['isSuccess']) 53 print(result1['isSuccess'])Copy the code

summary

In the actual work encountered problems to learn to look up information, see its corresponding official documents and source code, not only can play a multiplier role, but also can exercise their ability to solve problems. This point I have a deep experience!!

In order to facilitate you to see my blog posts on the mobile terminal, I have registered my personal wechat public account and can scan the QR code at the bottom left. You are welcome to pay attention to it. I will share relevant technical blog posts in time.

In order to facilitate the interaction and discussion of relevant technical issues, a special wechat group has been set up. Since the number of wechat groups is 100, please scan the QR code of Hongge’s personal wechat to get you into the group

(Please be sure to note: into the group)

Welcome to join this big family. Let’s swim the ocean of knowledge together.

Thank you for taking the time to read this article. If you think you have learned something from this article, it is also for rewarding the blogger for having a cup of coffee. Thank you! If you find reading this article helpful, please click the “Recommend” button in the lower left corner of your

“Recommended”

Will be my biggest writing motivation! In addition, you can also choose [focus on me], it is very convenient to find me! The copyright of this article belongs to the author and the blog park, the source website: www.cnblogs.com/du-hong welcome to reprint, but without the consent of the author, after reprint the article must be in the obvious position of the article page to give the author and the original link, otherwise reserve the right to pursue legal responsibility!