微博发布文章接口分析及模拟发送文章(Python)

微博发布文章接口分析及模拟发送文章

1. URL

https://weibo.com/aj/mblog/add?ajwvr=6&__rnd=1568601635935

1.1 请求方式

POST方法

1.2 查询字符串参数

  1. ajwvr:6 未知
  2. __rnd:1568601635935 13位时间戳

1.3 Post参数

  1. location:v6_content_home 疑似固定
  2. text:fffasfsdf 正文内容
  3. appkey:
  4. style_type:1
  5. pic_id: 图片id组,以“|”符分割
  6. tid:
  7. pdetail:
  8. mid:
  9. isReEdit:false 是否更新
  10. rank:0
  11. rankid:
  12. module:stissue
  13. pub_source:main_
  14. pub_type:dialog
  15. isPri:0
  16. _t:0
  17. updata_img_num:1 上传图片数量

1.4 请求头数据

Accept: */*
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.9
Connection:keep-alive
Content-Length:178
Content-Type:application/x-www-form-urlencoded
Cookie: ???(保密)
Host:weibo.com
Origin:https://weibo.com
Referer:https://weibo.com
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6788.400 QQBrowser/10.3.2843.400
X-Requested-With:XMLHttpRequest

1.5 响应头数据

Cache-Control:no-cache, must-revalidate
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/json; charset=utf-8
Date:Mon, 16 Sep 2019 02:40:36 GMT
DPOOL_HEADER:weibo_com11y116
Expires:Sat, 26 Jul 1997 05:00:00 GMT
LB_HEADER:venus242
Pragma:no-cache
Server:WeiBo/LB
Strict-Transport-Security:max-age=31536000; preload
Transfer-Encoding:chunked
Vary:Accept-Encoding

1.6 响应体数据

{
    "code":"100000",
    "msg":"",
    "data":{
        "html":""
    }
}

2. selenium获取登录cookies

2.1 selenium登录并将cookie存于pickle中

import time
import pickle
from selenium import webdriver

url='https://weibo.com'
user_name=input('用户名:')
password=input('密码:')

# 打开浏览器
browser=webdriver.Firefox()
browser.get(url)

# 输入用户名
while True:
    try:
        user_name_input=browser.find_element_by_id('loginname')
    except:
        pass
    else:
        time.sleep(5)
        break
    
user_name_input.clear()
user_name_input.send_keys(user_name)

# 输入密码
password_input=browser.find_element_by_name('password')
password_input.send_keys(password)

# 点击登录按钮
browser.find_element_by_xpath("//div[@class='info_list login_btn']").click()
time.sleep(8)

if '立即注册' not in browser.page_source:
    print('登录成功')
    b_cookies=browser.get_cookies()
    cookies=dict()
    for i in b_cookies:
        cookies[i['name']]=i['value']
    with open('miniblog_cookies.txt','wb') as f:
        f.write(pickle.dumps(cookies))
else:
    print('登录失败')

2.2 加载pickle化的cookie字典

# 获取cookies
with open('miniblog_cookies.txt','rb') as f:
    p_cookies=f.read()

miniblog_cookies=pickle.loads(p_cookies)

3. 上传图片接口

3.1 接口分析

URL: https://picupload.weibo.com/interface/pic_upload.php

3.2 查询字符串参数

  1. cb:https://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_156862032044992
  2. mime:image/jpeg 图片格式
  3. data:base64 base64方式
  4. url:weibo.com/u/用户id 用户id
  5. markpos:1
  6. logo:1
  7. nick:@用户名 用户名
  8. marks:0
  9. app:miniblog
  10. s:rdxt
  11. pri:null
  12. file_source:1

3.3 响应头参数

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.6.1
Date: Mon, 16 Sep 2019 07:52:22 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: https://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_156862032044992&ret=1&pid=007MyUzhgy1g71esl5nk7j30hs0hsdh8

3.4 请求头参数

Host: picupload.weibo.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate, br
Content-Type: application/x-www-form-urlencoded
Content-Length: 86775
Connection: keep-alive
Referer: https://weibo.com/u/7130514555/home?topnav=1&wvr=6
Cookie: 登录后的cookies
Upgrade-Insecure-Requests: 1

3.5 请求体POST参数

b64_data:(b64编码后的图片)

3.6 利用requests库模拟上传图片

# 如何b64编码图片
import base64

with open('test.jpg','rb') as f:
    img_bytes=f.read()

b64_data=base64.b64encode(img_bytes).decode('utf-8')
# 获取uid和nick
import re
from fake_useragent import UserAgent

user_info = dict()
index_url = 'https://weibo.com'
default_headers = {'Host': 'weibo.com',
                   'User-Agent': UserAgent().chrome}
r = requests.get(index_url, headers=default_headers, cookies=miniblog_cookies)
html = r.text
if "['islogin']='1'" in html:
    print('登录成功...')
    user_info['nick'] = re.search("\['nick'\]='(.*?)'", html).group(1)
    user_info['uid'] = re.search("\['uid'\]='(.*?)'", html).group(1)
    print(user_info)
else:
    print('登录失败...')
# 上传图片,并返回图片的id列表,以供发布文章使用
def upload_imgs(self, img_list):
    assert isinstance(img_list, list), 'img_list为list对象'

    img_ids = []
    headers = {'Host': 'picupload.weibo.com',
               'User-Agent': default_headers['User-Agent'],
               'Content-Type': 'application/x-www-form-urlencoded',
               'Upgrade-Insecure-Requests': '1',
               'Referer': f'https://weibo.com/u/{user_info["uid"]}/home?topnav=1&wvr=6'}

    for img_path in img_list:
        assert os.path.isfile(img_path), f'{img_path},非文件'
        with open(img_path, 'rb') as f:
            b64_data = base64.b64encode(f.read()).decode('utf-8')
            params = {
                'cb': f'https://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_{int(time.time()*100000)}',
                'mime': 'image/jpeg',
                'data': 'base64',
                'url': f'weibo.com/u/{user_info["uid"]}',
                'markpos': '1',
                'logo': '1',
                'nick': f'@{user_info["nick"]}',
                'marks': '0',
                'app': 'miniblog',
                's': 'rdxt',
                'pri': 'null',
                'file_source': '1'}
            data = {'b64_data': b64_data}
            r = requests.post(img_url, params=params, data=data, headers=headers, allow_redirects=False,
                              cookies=miniblog_cookies)
            img_id = re.search('pid=(.*)&?', r.headers['Location']).group(1)
            img_ids.append(img_id)

    return img_ids

5. 利用requests库模拟文章发送请求

# 发布文章
import requests

url_format='https://weibo.com/aj/mblog/add?ajwvr=6&__rnd={timestamp}'
url=url_format.format(timestamp=int(time.time()*1000))

post_data={'location': 'v6_content_home',
 'text': '测试',
 'appkey': '',
 'style_type': '1',
 'pic_id': '',
 'tid': '',
 'pdetail': '',
 'mid': '',
 'isReEdit': 'false',
 'rank': '0',
 'rankid': '',
 'module': 'stissue',
 'pub_source': 'main_',
 'pub_type': 'dialog',
 'isPri': '0',
 '_t': '0',
 'pic_id':'|'.join(img_ids),
 'updata_img_num':str(len(img_ids))}

headers={'Host':'weibo.com',
        'Origin':'https://weibo.com',
        'Referer':'https://weibo.com',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6788.400 QQBrowser/10.3.2843.400',
        'X-Requested-With':'XMLHttpRequest'}

r=requests.post(url,data=post_data,headers=headers,cookies=miniblog_cookies)
r.encoding='utf-8'
print(r.text)

6. 最终效果图(未成年人别看了…)

微博发布文章接口分析及模拟发送文章(Python)_第1张图片

你可能感兴趣的:(Python杂谈)