python接口自动化-session_自动发文

一、session简介

查看 requests.session() 帮助文档(只贴了一部分内容)

import requests
help(requests.session())

class Session(SessionRedirectMixin)
 |  A Requests session.
 |  
 |  Provides cookie persistence, connection-pooling, and configuration.
 |  
 |  Basic Usage::
 |  
 |    >>> import requests
 |    >>> s = requests.Session()
 |    >>> s.get('http://httpbin.org/get')
 |    
 |  
 |  Or as a context manager::
 |  
 |    >>> with requests.Session() as s:
 |    >>>     s.get('http://httpbin.org/get')
 |    

  

二、使用session登录

博客园登录实操:

# coding:utf-8
import requests
'''
https的请求相对于http安全级别高,需要验证SSL证书
import urllib3  使用这个方法就OK了
urllib3.disable_warnings()  忽略警告
'''
import urllib3
urllib3.disable_warnings()

url = "https://passport.cnblogs.com/user/signin"

headers = {
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Content-Type": "application/json; charset=utf-8",  #json格式
            "X-Requested-With": "XMLHttpRequest",
            "Referer": "https://passport.cnblogs.com/user/signin",
            "Accept-Language": "zh-CN",
            "Accept-Encoding": "gzip, deflate",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
            "Connection": "Keep-Alive",
            "Cookie": "_ga=GA1.2.2031405227.1553413697; _gid=GA1.2.1694843763.1553413697; "
                      "ASP.NET_SessionId=jw21lhtlzwfs14grozrtn5es; "
                      "SERVERID=4fea726178f35f0633c3d1a5c08ace19|1553430739|1553430738",
            }

payload = {
           "input1":"k2bbCom4IU6eUoLehhL1l+uvFscRoUS5V9ZXmiiRnls"
                    "jS1fhvbTbj+sVg46vjJ6n3hm2kTVfx7O+dJh9+s7Fv"
                    "sWbNg1boYxn+mF2QdOoLBT6Zx4debvK3ieMaolFvCZH"
                    "gggaP+lvB1boSxMvfbKjjhB0R1anz72zyN1OUhfuitU=",
           "input2":"iK6m5phf0al626Sfn/mKzAunzXlmaY65X5WX4hha67"
                    "cp1iS81fUmp5TwP6y3XZt7SRHStQ147xR/jMeAcjPnD"
                    "H5nhnQeDispR6ZAgmEd8bjInoc81tAKycOmlqBGNeCOj"
                    "PweXlcR8pREJhm7iSPPHqmN8GJ4c7GGc5C/eZc4Uks=",
           "remember":True
           }

s = requests.session()
r = s.post(url, json=payload, headers=headers, verify=False)
print(r.json())

 返回的结果:

python接口自动化-session_自动发文_第1张图片

Fiddler中的结果:

python接口自动化-session_自动发文_第2张图片

 

三、自动发文,保存草稿博客

1.先打开登录首页,刷新一下,fiddler抓包,获取部分cookie

python接口自动化-session_自动发文_第3张图片

代码:

# coding:utf-8
import requests
import urllib3
urllib3.disable_warnings()
# 先打开登录首页,刷新一下,fiddler抓包,获取部分cookie
url = "https://passport.cnblogs.com/user/signin"
headers = {
            "Accept": "text/html, application/xhtml+xml, */*",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
            "Accept-Encoding": "gzip, deflate",
            "Connection": "Keep-Alive",
            }
s = requests.session()
r = s.get(url, headers=headers, verify=False)
print("第一次打印")
print(s.cookies)

  

2.添加登录需要的两个cookie

代码:

c = requests.cookies.RequestsCookieJar()

c.set('.CNBlogsCookie', '25D6E068A4434E2532E9F553728ACDE1F94F18E8079E07660FA1'
                        '68C361D87E1A59533A6BA991A7B14091F6609057C32CDE3E4BEDFD'
                        'DEEA6684C7E5F96A98B77C106640480FA5B3A78EC4EEC76E9E2D5F60082CE6')  # 填登录后的抓包内容
c.set('.Cnblogs.AspNetCore.Cookies', 'CfDJ8JcopKY7yQlPr3eegllP76PlTM6nN56TphTAQZU5'
                                     'OFZAKIG6zQQZsZpvJ4y7IBNq4M87KdPYiThIpRzGCp0qaG'
                                     'Q6s30ZdqMvlqDRI0dM2k_jUprCUQ-6FZsKkco_lYEz1v9QQ'
                                     'I_NIl85NQgwqXbjgOmErQIMWUOqhp9_zd1lVQEctfJ8Dt7aa'
                                     'UtO-YSPZ8glHRGQuvXghskz3MOk1IdDdnbUsytc15vJXTKtY'
                                     '-b7HmN2KbsMkwnnF2MSWHztAOYNU8AIuZXS3mX-UrG9PEi0'
                                     '2UTfjkaJltqX4RwddTjTk2DI')  # 填登录后的抓包内容
c.set('AlwaysCreateItemsAsActive', "True")
c.set('AdminCookieAlwaysExpandAdvanced', "True")

s.cookies.update(c)
print("第二次打印")
print(s.cookies)

 

3.登录成功后访问编辑文章

r1 = s.get("https://i.cnblogs.com/EditPosts.aspx?opt=1", headers=headers, verify=False)

 

4.打开新随笔,输入内容后保存为草稿,用fiddler抓包

python接口自动化-session_自动发文_第4张图片

python接口自动化-session_自动发文_第5张图片

从博客园处可以查看到:

python接口自动化-session_自动发文_第6张图片

 

5.把 body 的参数内容写成字典格式,有几个空的参数不是必填项,可以去掉

body = {"__VIEWSTATE": "",
        "__VIEWSTATEGENERATOR": "FE27D343",
        "Editor$Edit$Advanced$chkComments": "on",
        "Editor$Edit$Advanced$chkDisplayHomePage": "on",
        "Editor$Edit$Advanced$chkMainSyndication": "on",
        "Editor$Edit$Advanced$ckbPublished": "on",
        "Editor$Edit$APOptions$Advancedpanel1$cklCategories$2": "1426593",
        "Editor$Edit$EditorBody": "

jjjjjjjjjjjjjjjjjjjjjjjjjj" "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" "jjjjjjrrrrrrrrrrrrrrrrrrrrrrrrr" "rrrrrrrrrrrrrrrrrr

", "Editor$Edit$lkbDraft": "存为草稿", "Editor$Edit$txbTitle": "testinggggggggg", }

  

6.用上面的 session 继续发送 post 请求,对参数稍作修改

url2 = "https://i.cnblogs.com/EditPosts.aspx?opt=1"

body = {"__VIEWSTATE": "",
        "__VIEWSTATEGENERATOR": "FE27D343",
        "Editor$Edit$Advanced$chkComments": "on",
        "Editor$Edit$Advanced$chkDisplayHomePage": "on",
        "Editor$Edit$Advanced$chkMainSyndication": "on",
        "Editor$Edit$Advanced$ckbPublished": "on",
        "Editor$Edit$APOptions$Advancedpanel1$cklCategories$2": "1426593",
        "Editor$Edit$EditorBody": "

222222222222jjjjjjjjjjjjjjjjjjjjjjjjjj" "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" "jjjjjjrrrrrrrrrrrrrrrrrrrrrrrrr" "rrrrrrrrrrrrrrrrrr

", "Editor$Edit$lkbDraft": "存为草稿", "Editor$Edit$txbTitle": "testinggggggggg2222222222", } r2 = s.post(url2, data=body, verify=False) print("第三次打印") print(r.content.decode("utf-8"))

  

7.执行后,查看我的博客,就新增了一条草稿内容

python接口自动化-session_自动发文_第7张图片

详细内容:

python接口自动化-session_自动发文_第8张图片

 

到这里整体的自动发文就完成了

 

四、全部代码参考

# coding:utf-8
import requests
import urllib3
urllib3.disable_warnings()
# 先打开登录首页,刷新一下,fiddler抓包,获取部分cookie
url = "https://passport.cnblogs.com/user/signin"
headers = {
            "Accept": "text/html, application/xhtml+xml, */*",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
            "Accept-Encoding": "gzip, deflate",
            "Connection": "Keep-Alive",
            }
s = requests.session()
r = s.get(url, headers=headers, verify=False)
print("第一次打印")
print(s.cookies)

# 添加登录需要的两个cookie
c = requests.cookies.RequestsCookieJar()

c.set('.CNBlogsCookie', '25D6E068A4434E2532E9F553728ACDE1F94F18E8079E07660FA1'
                        '68C361D87E1A59533A6BA991A7B14091F6609057C32CDE3E4BEDFD'
                        'DEEA6684C7E5F96A98B77C106640480FA5B3A78EC4EEC76E9E2D5F60082CE6')  # 填登录后的抓包内容
c.set('.Cnblogs.AspNetCore.Cookies', 'CfDJ8JcopKY7yQlPr3eegllP76PlTM6nN56TphTAQZU5'
                                     'OFZAKIG6zQQZsZpvJ4y7IBNq4M87KdPYiThIpRzGCp0qaG'
                                     'Q6s30ZdqMvlqDRI0dM2k_jUprCUQ-6FZsKkco_lYEz1v9QQ'
                                     'I_NIl85NQgwqXbjgOmErQIMWUOqhp9_zd1lVQEctfJ8Dt7aa'
                                     'UtO-YSPZ8glHRGQuvXghskz3MOk1IdDdnbUsytc15vJXTKtY'
                                     '-b7HmN2KbsMkwnnF2MSWHztAOYNU8AIuZXS3mX-UrG9PEi0'
                                     '2UTfjkaJltqX4RwddTjTk2DI')  # 填登录后的抓包内容
c.set('AlwaysCreateItemsAsActive', "True")
c.set('AdminCookieAlwaysExpandAdvanced', "True")

s.cookies.update(c)
print("第二次打印")
print(s.cookies)

# 登录成功后访问编辑文章
r1 = s.get("https://i.cnblogs.com/EditPosts.aspx?opt=1", headers=headers, verify=False)

# 保存草稿箱
url2 = "https://i.cnblogs.com/EditPosts.aspx?opt=1"

body = {"__VIEWSTATE": "",
        "__VIEWSTATEGENERATOR": "FE27D343",
        "Editor$Edit$Advanced$chkComments": "on",
        "Editor$Edit$Advanced$chkDisplayHomePage": "on",
        "Editor$Edit$Advanced$chkMainSyndication": "on",
        "Editor$Edit$Advanced$ckbPublished": "on",
        "Editor$Edit$APOptions$Advancedpanel1$cklCategories$2": "1426593",
        "Editor$Edit$EditorBody": "

222222222222jjjjjjjjjjjjjjjjjjjjjjjjjj" "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" "jjjjjjrrrrrrrrrrrrrrrrrrrrrrrrr" "rrrrrrrrrrrrrrrrrr

", "Editor$Edit$lkbDraft": "存为草稿", "Editor$Edit$txbTitle": "testinggggggggg2222222222", } r2 = s.post(url2, data=body, verify=False) print("第三次打印") print(r.content.decode("utf-8"))

  

运行后返回的结果:

F:\test-req-py\venv\Scripts\python.exe F:/test-req-py/day2/t4.py
第一次打印
, , ]>
第二次打印
, , , , , , ]>
第三次打印




    
    
    
    
    
    用户登录 - 博客园
    ...内容太多,省略...



 

转载于:https://www.cnblogs.com/JodieRao/p/10589130.html

你可能感兴趣的:(python接口自动化-session_自动发文)