码迷,mamicode.com
首页 > 编程语言 > 详细

python接口自动化3-自动发帖(session)

时间:2018-05-12 10:23:29      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:nbsp   pre   contex   tco   其它   asi   img   creat   input   

上一篇模拟登录博客园,但这只是第一步,一般登录后,还会有其它的操作,如发帖,评论等,这时候如何保持会话呢?

 (敲黑板!!!由于博客园最近登录机制变了,登录全部走cookie登录)

一、session简介

1.查看帮助文档,贴了一部分,后面省略了

>>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]>

 

二、使用session登录

1.使用session登录只需在上一篇基础上稍做修改

# coding:utf-8
import requests
url = "https://passport.cnblogs.com/user/signin"

headers = {
                  头部信息已省略

                   }
payload = {"input1":"xxx",
                "input2":"xxx",
                "remember":True}

# r = requests.post(url, json=payload, headers=headers,verify=False)

# 修改后如下
s = requests.session()
r = s.post(url, json=payload, headers=headers,verify=False)
print r.json()

三、保存编辑

1.先打开我的随笔,手动输入内容后,打开fiddler抓包

技术分享图片

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

body = {"__VIEWSTATE": "",
        "__VIEWSTATEGENERATOR":"FE27D343",
        "Editor$Edit$txbTitle":"这是我的标题:上海-悠悠",
        "Editor$Edit$EditorBody":"<p>这里是中文内容:http://www.cnblogs.com/yoyoketang/</p>",
        "Editor$Edit$Advanced$ckbPublished":"on",
        "Editor$Edit$Advanced$chkDisplayHomePage":"on",
        "Editor$Edit$Advanced$chkComments":"on",
        "Editor$Edit$Advanced$chkMainSyndication":"on",
        "Editor$Edit$lkbDraft":"存为草稿",
         }

3.用上面的session继续发送post请求

技术分享图片

4.执行后,查看我的草稿箱就多了一条新增的了

技术分享图片

 

四、参考代码

 1  # coding:utf-8
 2 import requests
 3 
 4 # 先打开登录首页,获取部分cookie
 5 url = "https://passport.cnblogs.com/user/signin"
 6 headers = {
 7             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"
 8            }  # get方法其它加个ser-Agent就可以了
 9 
10 s = requests.session()
11 r = s.get(url, headers=headers,verify=False)
12 print s.cookies
13 
14 # 添加登录需要的两个cookie
15 c = requests.cookies.RequestsCookieJar()
16 
17 c.set(.CNBlogsCookie, 这里是抓到的)  # 填上面抓包内容
18 c.set(.Cnblogs.AspNetCore.Cookies,这里是抓到的)  # 填上面抓包内容
19 c.set(AlwaysCreateItemsAsActive,"True")
20 c.set(AdminCookieAlwaysExpandAdvanced,"True")
21 s.cookies.update(c)
22 print s.cookies
23 
24 # 登录成功后保存编辑内容
25 r1 = s.get("https://i.cnblogs.com/EditPosts.aspx?opt=1", headers=headers, verify=False)
26 
27 # 保存草稿箱
28 url2= "https://i.cnblogs.com/EditPosts.aspx?opt=1"
29 body = {"__VIEWSTATE": "",
30         "__VIEWSTATEGENERATOR":"FE27D343",
31         "Editor$Edit$txbTitle":"这是3111",
32         "Editor$Edit$EditorBody":"<p>这里111:http://www.cnblogs.com/yoyoketang/</p>",
33         "Editor$Edit$Advanced$ckbPublished":"on",
34         "Editor$Edit$Advanced$chkDisplayHomePage":"on",
35         "Editor$Edit$Advanced$chkComments":"on",
36         "Editor$Edit$Advanced$chkMainSyndication":"on",
37         "Editor$Edit$Advanced$txbEntryName":"",
38         "Editor$Edit$Advanced$txbExcerpt":"",
39         "Editor$Edit$Advanced$tbEnryPassword":"",
40         "Editor$Edit$lkbDraft":"存为草稿",
41          }
42 r2 = s.post(url2, data=body, verify=False)
43 print r.content

 

python接口自动化3-自动发帖(session)

标签:nbsp   pre   contex   tco   其它   asi   img   creat   input   

原文地址:https://www.cnblogs.com/jason89/p/9027691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!