python接口自動化-session_自動發文

1、session簡介javascript

查看 requests.session() 幫助文檔(只貼了一部份內容)html

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')
 |    <Response [200]>
 |  
 |  Or as a context manager::
 |  
 |    >>> with requests.Session() as s:
 |    >>>     s.get('http://httpbin.org/get')
 |    <Response [200]>

  

2、使用session登陸java

博客園登陸實操:python

# 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())

 返回的結果:web

Fiddler中的結果:chrome

 

3、自動發文,保存草稿博客json

1.先打開登陸首頁,刷新一下,fiddler抓包,獲取部分cookie安全

代碼:cookie

# 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.添加登陸須要的兩個cookiesession

代碼:

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抓包

從博客園處能夠查看到:

 

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": "<p>jjjjjjjjjjjjjjjjjjjjjjjjjj"
                                  "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"
                                  "jjjjjjrrrrrrrrrrrrrrrrrrrrrrrrr"
                                  "rrrrrrrrrrrrrrrrrr</p>",
        "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": "<p>222222222222jjjjjjjjjjjjjjjjjjjjjjjjjj"
                                  "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"
                                  "jjjjjjrrrrrrrrrrrrrrrrrrrrrrrrr"
                                  "rrrrrrrrrrrrrrrrrr</p>",
        "Editor$Edit$lkbDraft": "存爲草稿",
        "Editor$Edit$txbTitle": "testinggggggggg2222222222",
        }
r2 = s.post(url2, data=body, verify=False)
print("第三次打印")
print(r.content.decode("utf-8"))

  

7.執行後,查看個人博客,就新增了一條草稿內容

詳細內容:

 

到這裏總體的自動發文就完成了

 

4、所有代碼參考

# 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": "<p>222222222222jjjjjjjjjjjjjjjjjjjjjjjjjj"
                                  "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"
                                  "jjjjjjrrrrrrrrrrrrrrrrrrrrrrrrr"
                                  "rrrrrrrrrrrrrrrrrr</p>",
        "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
第一次打印
<RequestsCookieJar[<Cookie ASP.NET_SessionId=z4hfksxhg2j2l0s5yuvbrljc for passport.cnblogs.com/>, <Cookie AspxAutoDetectCookieSupport=1 for passport.cnblogs.com/>, <Cookie SERVERID=33fdc71a64f9cf564b6409a0da1aa10e|1553434512|1553434512 for passport.cnblogs.com/>]>
第二次打印
<RequestsCookieJar[<Cookie .CNBlogsCookie=25D6E068A4434E2532E9F553728ACDE1F94F18E8079E07660FA168C361D87E1A59533A6BA991A7B14091F6609057C32CDE3E4BEDFDDEEA6684C7E5F96A98B77C106640480FA5B3A78EC4EEC76E9E2D5F60082CE6 for />, <Cookie .Cnblogs.AspNetCore.Cookies=CfDJ8JcopKY7yQlPr3eegllP76PlTM6nN56TphTAQZU5OFZAKIG6zQQZsZpvJ4y7IBNq4M87KdPYiThIpRzGCp0qaGQ6s30ZdqMvlqDRI0dM2k_jUprCUQ-6FZsKkco_lYEz1v9QQI_NIl85NQgwqXbjgOmErQIMWUOqhp9_zd1lVQEctfJ8Dt7aaUtO-YSPZ8glHRGQuvXghskz3MOk1IdDdnbUsytc15vJXTKtY-b7HmN2KbsMkwnnF2MSWHztAOYNU8AIuZXS3mX-UrG9PEi02UTfjkaJltqX4RwddTjTk2DI for />, <Cookie AdminCookieAlwaysExpandAdvanced=True for />, <Cookie AlwaysCreateItemsAsActive=True for />, <Cookie ASP.NET_SessionId=z4hfksxhg2j2l0s5yuvbrljc for passport.cnblogs.com/>, <Cookie AspxAutoDetectCookieSupport=1 for passport.cnblogs.com/>, <Cookie SERVERID=33fdc71a64f9cf564b6409a0da1aa10e|1553434512|1553434512 for passport.cnblogs.com/>]>
第三次打印

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <meta name="renderer" content="webkit" />
    <meta name="force-rendering" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>用戶登陸 - 博客園</title>
    ...內容太多,省略...
</head>
</body>
</html>
相關文章
相關標籤/搜索