最新消息:20190529 VPS服务器已从腾讯云香港换为Vultr新加坡,主题仍用朋友推荐的大前端D8

【已解决】Python中如何设置requests中的cookie等参数去模拟访问获取QQ空间的接口

Python crifan 462浏览 0评论

折腾:

【未解决】用Chrome去模拟和分析QQ空间登录的逻辑

期间,需要想办法去用requests去模拟之前分析出来的:

<code>    1. Request URL:
https://user.qzone.qq.com/proxy/domain/b.qzone.qq.com/cgi-bin/blognew/get_abs?hostUin=&uin=&blogType=0&cateName=&cateHex=&statYear=2018&reqInfo=7&pos=0&num=15&sortType=0&source=0&rand=0.7794242902205004&ref=qzone&g_tk=1381451249&verbose=1&qzonetoken=b4f3d7b4fd7f5f439a116fa9f0970f34f093c38f94b4aa0c161782354e8d6bcf9d2ee01b0daa11fc9b
    2. Request Method:
GET
    3. Status Code:
200
    4. Remote Address:
180.163.21.59:443
    5. Referrer Policy:
no-referrer-when-downgrade
1. Response Headers
    1. cache-control:
max-age=3600
    2. content-encoding:
gzip
    3. content-length:
844
    4. content-type:
application/x-javascript; charset=gbk
    5. date:
Sun, 23 Dec 2018 13:13:28 GMT
    6. mod-map:
proxy_domain:photo.v7/module/proxy/sync.js
    7. server:
QZHTTP-2.38.18
    8. status:
200
    9. vary:
Origin, Accept
    10. x-powered-by:
TSW/Node.js
    11. x-stgw-ssl-info:
099ce04b0358a5aebc2b554f22cf899c|0.212|1545570808.035|4|r|I|TLSv1.2|ECDHE-RSA-AES128-GCM-SHA256|20500|0
2. Request Headers
    1. :authority:
user.qzone.qq.com
    2. :method:
GET
    3. :path:
/proxy/domain/b.qzone.qq.com/cgi-bin/blognew/get_abs?hostUin=&amp;uin=&amp;blogType=0&amp;cateName=&amp;cateHex=&amp;statYear=2018&amp;reqInfo=7&amp;pos=0&amp;num=15&amp;sortType=0&amp;source=0&amp;rand=0.7794242902205004&amp;ref=qzone&amp;g_tk=1381451249&amp;verbose=1&amp;qzonetoken=b4f3d7b4fd7f5f439a116fa9f0970f34f093c38f94b4aa0c161782354e8d6bcf9d2ee01b0daa11fc9b
    4. :scheme:
https
    5. accept:
*/*
    6. accept-encoding:
gzip, deflate, br
    7. accept-language:
zh-CN,zh;q=0.9,en;q=0.8
    8. cache-control:
no-cache
    9. cookie:
welcomeflash=20050606_107001; zzpaneluin=; zzpanelkey=; pgv_pvi=7640393728; pgv_si=s3147298816; pgv_pvid=3951804270; pgv_info=ssid=s7487670374; ptisp=ctc; ptui_loginuin=; pt2gguin=o; uin=o; skey=@nDTkOJm1m; RK=Ye5Jmtb0ly; ptcz=3b6806bf7cddc375bc2d23b04ff9c366b47fac808b9256322ad69a23f2dc580f; p_uin=o; pt4_token=aC5vGfNAA2M3fS7ngcAHdXoiCvqwrAGcEuL54gs63oE_; p_skey=kSC7q75Gk93gLlo*mRMg*h2m3iYUuubjQqVBIgEMi*o_; Loading=Yes
    10. pragma:
no-cache
    11. referer:
https://qzs.qq.com/qzone/app/blog/v6/bloglist.html
    12. user-agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
3. Query String Parametersview sourceview URL encoded
    1. hostUin:

    2. uin:

    3. blogType:
0
    4. cateName:
    5. cateHex:
    6. statYear:
2018
    7. reqInfo:
7
    8. pos:
0
    9. num:
15
    10. sortType:
0
    11. source:
0
    12. rand:
0.7794242902205004
    13. ref:
qzone
    14. g_tk:
1381451249
    15. verbose:
1
    16. qzonetoken:
b4f3xxxdaa11fc9b
</code>

看看这些参数全部设置了之后,调用接口会返回什么结果

是否是cookie过期了?需要重新登录?

还是或许可以得到要的结果

先要搞清楚,requests中如何设置cookie

requests set cookie

How to send cookies in a post request with the Python Requests library? – Stack Overflow

requests.cookies — Requests 2.21.0 documentation

快速上手 — Requests 2.18.1 文档

“>>> url = ‘http://httpbin.org/cookies’

>>> cookies = dict(cookies_are=’working’)

>>> r = requests.get(url, cookies=cookies)

>>> r.text

‘{“cookies”: {“cookies_are”: “working”}}'”

和:

“>>> jar = requests.cookies.RequestsCookieJar()

>>> jar.set(‘tasty_cookie’, ‘yum’, domain=’httpbin.org’, path=’/cookies’)

>>> jar.set(‘gross_cookie’, ‘blech’, domain=’httpbin.org’, path=’/elsewhere’)

>>> url = ‘http://httpbin.org/cookies’

>>> r = requests.get(url, cookies=jar)

>>> r.text

‘{“cookies”: {“tasty_cookie”: “yum”}}'”

How to add a cookie to the cookiejar in python requests library – Stack Overflow

Send Cookie Using Python Requests – Stack Overflow

高级用法 — Requests 2.18.1 文档

先去试试

代码:

<code>import requests
import codecs

def saveTextToFile(filename, textToSave, fileEncoding="utf-8"):
    """save text info file"""
    with codecs.open(filename, 'w', encoding=fileEncoding) as fp:
        fp.write(textToSave)
        fp.close()


def blogGetAbs():
    getAbsUrl = "https://user.qzone.qq.com/proxy/domain/b.qzone.qq.com/cgi-bin/blognew/get_abs?hostUin=xx&amp;uin=xx&amp;blogType=0&amp;cateName=&amp;cateHex=&amp;statYear=2018&amp;reqInfo=7&amp;pos=0&amp;num=15&amp;sortType=0&amp;source=0&amp;rand=0.7794242902205004&amp;ref=qzone&amp;g_tk=1381451249&amp;verbose=1&amp;qzonetoken=b4f3d7b4fd7f5f439a116fa9f0970f34f093c38f94b4aa0c161782354e8d6bcf9d2ee01b0daa11fc9b"
    """
welcomeflash=20050606_107001; zzpaneluin=; zzpanelkey=; pgv_pvi=7640393728; pgv_si=s3147298816; pgv_pvid=3951804270; pgv_info=ssid=s7487670374; ptisp=ctc; ptui_loginuin=xx; pt2gguin=xx; uin=oxx; skey=@nDTkOJm1m; RK=Ye5Jmtb0ly; ptcz=3b6806bf7cddc375bc2d23b04ff9c366b47fac808b9256322ad69a23f2dc580f; p_uin=oxx7; pt4_token=aC5vGfNAA2M3fS7ngcAHdXoiCvqwrAGcEuL54gs63oE_; p_skey=kSC7q75Gk93gLlo*mRMg*h2m3iYUuubjQqVBIgEMi*o_; Loading=Yes
    """
    cookieDict = {
        "welcomeflash": "20050606_107001",
        "zzpaneluin": "",
        "zzpanelkey": "",
        "pgv_pvi": "7640393728",
        "pgv_si": "s3147298816",
        "pgv_pvid": "3951804270",
        "pgv_info": "ssid=s7487670374",
        "ptisp": "ctc",
        "ptui_loginuin": "xx",
        "pt2gguin": "oxxx",
        "uin": "oxxx",
        "skey": "@nDTkOJm1m",
        "RK": "Ye5Jmtb0ly",
        "ptcz": "3b6806bf7cddc375bc2d23b04ff9c366b47fac808b9256322ad69a23f2dc580f",
        "p_uin": "oxxx",
        "pt4_token": "aC5vGfNAA2M3fS7ngcAHdXoiCvqwrAGcEuL54gs63oE_",
        "p_skey": "kSC7q75Gk93gLlo*mRMg*h2m3iYUuubjQqVBIgEMi*o_",
        "Loading": "Yes;",
    }
    resp = requests.get(getAbsUrl, cookies=cookieDict)
    print("resp=%s", resp)
    respHtml = resp.text
    saveTextToFile("get_abs.html", respHtml, resp.encoding)
</code>

竟然真的能获得我们要的数据:

<code>_Callback({
  "code": 0,
  "subcode": 0,
  "message": "",
  "default": 0,
  "data": {
    "totalNum": 1488,
    "hostFlag": 0,
    "list": [{
      "blogId": 1438799989,
      "blogType": 0,
      "pubTime": "2015-08-06 02:39",
      "cate": "个人日记",
      "cateHex": "b8f6c8cbc8d5bcc7",
      "title": "休息一天",
      "commentNum": 0,
      "effect1": 1281,
      "effect2": 524294,
      "ar": 0,
      "block": 256,
      "inproc": false,
      "appeal": 1,
      "artype": 1,
      "arUins": ""
    }, ...
      "blogId": 1544544190,
      "blogType": 0,
      "pubTime": "2018-12-12 00:03",
      "cate": "个人日记",
      "cateHex": "b8f6c8cbc8d5bcc7",
      "title": "看手机",
      "commentNum": 0,
      "effect1": 1025,
      "effect2": 524294,
      "ar": 0,
      "block": 0,
      "inproc": false,
      "appeal": 0,
      "artype": 1,
      "arUins": ""
    }],
    "cateInfo": {
      "blogNum": 1488,
      "catgoryNum": 1,
      "categoryList": [{
        "category": "个人日记",
        "cateHex": "b8f6c8cbc8d5bcc7",
        "num": 1488
      }]
    },
    "month_num": [31, 28, 32, 31, 30, 30, 32, 31, 31, 31, 29, 23]
  }
});
</code>

【总结】

此处暂时用:

<code>import requests

cookieDict = {
    "welcomeflash": "20050606_107001",
    ...
    "Loading": "Yes;",
}
resp = requests.get(getAbsUrl, cookies=cookieDict)
</code>

即可正常传递cookie了

-》此处是QQ空间的登录后的cookie

-》竟然还真的没有失效,可以获取到想要的数据呢。

转载请注明:在路上 » 【已解决】Python中如何设置requests中的cookie等参数去模拟访问获取QQ空间的接口

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.108 seconds, using 18.88MB memory