https://weibo.com/aj/mblog/add?ajwvr=6&__rnd=1568601635935
POST方法
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
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
{
"code":"100000",
"msg":"",
"data":{
"html":""
}
}
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('登录失败')
# 获取cookies
with open('miniblog_cookies.txt','rb') as f:
p_cookies=f.read()
miniblog_cookies=pickle.loads(p_cookies)
URL: https://picupload.weibo.com/interface/pic_upload.php
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
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
b64_data:(b64编码后的图片)
# 如何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
# 发布文章
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)