python (1) 获取cookie和使用cookie

获取cookie

方法一 requests.utils.dict_from_cookiejar()(推荐)

使用requests.utils.dict_from_cookiejar()把返回的cookies转换成字典

def get_cookie():
    url = "http://xxx"
    header = {
    	"Content-Type": "application/json; charset=UTF-8",
        "xxx": "xxx"
    }
    body = {
    	"xxx": "xxx", 
    	"xxx": "xxx"}
    try:
	    res = requests.post(url, headers=header, json=body, verify=False)
	    cookie = requests.utils.dict_from_cookiejar(res.cookies)
	    cookie = "c-token=" + cookie["c-token"]
	    return cookie
	except Exception as err:
        print('获取cookie失败:\n{0}'.format(err))

方法二 遍历拼接

遍历cookies的键值,拼接成cookie格式

def get_cookie():
    url = "http://xxx"
    header = {
    	"Content-Type": "application/json; charset=UTF-8",
        "xxx": "xxx"
    }
    body = {
    	"xxx": "xxx", 
    	"xxx": "xxx"}
    try:
	    res = requests.post(url, headers=header, json=body, verify=False)
	    cookies = res.cookies.items()
        cookie = ''
        for name, value in cookies:
            cookie += '{0}={1};'.format(name, value)
        return cookie
    except Exception as err:    
        print('获取cookie失败:\n{0}'.format(err))

使用cookie

方法一

import requests

def get_data():
    cookie = get_cookie()
    res = requests.get(url=xxxxx, cookies=cookie)
    print(res.text)

比较简洁高效

方法二

import requests

def get_data():
    cookie = get_cookie()
    headers = {"cookie": cookie}
    res = requests.get(url=xxxxxx, headers=headers)
    print(res.text)

更符合一般的表达习惯

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取抖音用户的关注列表,可以使用Python的第三方库requests和抖音的私有API接口。具体步骤如下: 1. 通过requests库向抖音的API接口发送请求,获取用户的uid。 2. 使用uid作为参数,向另一个API接口发送请求,获取该用户关注列表的加密参数sec_uid和max_time。 3. 使用获取到的sec_uid和max_time作为参数,再次发送请求,获取该用户的关注列表。 示例代码如下: ``` import requests import json import time import hashlib # 抖音API接口 api_url = 'https://aweme.snssdk.com' # 请求头信息 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'Accept': 'application/json', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded', 'Referer': 'https://www.douyin.com/', } # 用户名 user_name = 'xxxx' # 请求参数 params = { 'device_id': 'xxxx', 'iid': 'xxxx', 'openudid': 'xxxx', 'uid': '', 'count': '20', 'max_time': '', 'sec_user_id': '', 'source': 'follow', 'type': '0', 'cursor': '0', 'verifyFp': '', } # 获取用户uid url = api_url + '/aweme/v1/user/?user_name=' + user_name response = requests.get(url=url, headers=headers) data = json.loads(response.text) uid = data['user']['uid'] # 获取加密参数 url = api_url + '/aweme/v1/user/following/list/?user_id=' + uid response = requests.get(url=url, headers=headers) data = json.loads(response.text) sec_uid = data['extra']['now'] max_time = str(int(time.time() * 1000)) # 获取关注列表 url = api_url + '/aweme/v1/user/following/list/?user_id=' + uid + '&sec_user_id=' + sec_uid + '&max_time=' + max_time response = requests.get(url=url, headers=headers) data = json.loads(response.text) for user in data['followings']: print(user['nickname']) ``` 需要注意的是,抖音API接口是私有的,可能会随时变更。同时,使用API接口获取数据需要模拟请求头信息和加密参数等信息,需要一定的技术功底。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值