python使用新浪微博api上传图片到微博示例
发布时间:2021-10-13 02:15:06
import urllib.parse,os.path,time,sys
from http.client import HTTPSConnection
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
#path
ospath=sys.path[0]
if len(ospath)!=3:
ospath+='\\'
ospath=ospath.replace('\\','/')
#api
class Api:
def sina(self,status,pic):
fSize=os.path.getsize(pic)
BOUNDARY="$-img-lufei-goodboy-$"
CRLF='\r\n'
data=[
#token
'--'+BOUNDARY,
'Content-disposition: form-data; name="access_token"',
'',
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',#你的access_token
#status
'--'+BOUNDARY,
'Content-disposition: form-data; name="status"',
'',
status,
#pic
'--'+BOUNDARY,
'Content-disposition: form-data; name="pic"; filename="q_17.jpg"',
'Content-type: image/jpeg',
''
]
#utf-8
data=(CRLF.join(data)+CRLF).encode('utf-8')
closing='\r\n--'+BOUNDARY+'--\r\n'
sumlen=len(data)+len(closing)+fSize
#----------------------------------------
h=HTTPSConnection('upload.api.weibo.com')
h.putrequest('POST','/2/statuses/upload.json')
h.putheader('Content-type','multipart/form-data; boundary=%s' % BOUNDARY)
h.putheader('Content-length',sumlen)
h.endheaders()
h.send(data)
f=open(pic,'rb')
while True:
data=f.read(12345)
if not data:
break
h.send(data)
f.close()
h.send(closing.encode('utf-8'))
r=h.getresponse()
return r.read().decode('utf-8','ignore')
api=Api()
#ui
class Dialog(QDialog):
def __init__(self):
super().__init__()
#icon,title
self.setWindowIcon(QIcon(ospath+'weibo.ico'))
self.setWindowTitle('weibo')
#texteditor
self.editor=QTextEdit()
#textline,filebutton,submit
self.line=QLineEdit()
brows=QPushButton('打开')
brows.clicked.connect(self.getFileName)
submit=QPushButton('发表')
submit.clicked.connect(self.submit)
#layout
layout=QGridLayout()
layout.setContentsMargins(0,0,0,0)
#addwidget
layout.addWidget(self.editor,0,0,1,2)
layout.addWidget(self.line,1,0,1,1)
layout.addWidget(brows,1,1,1,1)
layout.addWidget(submit,2,0,1,2)
#set
self.setLayout(layout)
def getFileName(self):
fileName=QFileDialog.getOpenFileName()
self.line.setText(fileName[0])
def submit(self):
status=self.editor.toPlainText()
pic=self.line.text()
self.editor.setText(api.sina(status,pic))
app=QApplication(sys.argv)
dialog=Dialog()
dialog.show()
app.exec_()


猜你喜欢
- 今天遇到一个蛮奇怪的问题:当我在控制台中使用 urllib 没问题,但是当我在 vscode 中 .py 文件中导入 urllib 使用时会
- 环境: 开发的IDE:JBuilderX 使用的数据库:MS Sql Server 2000 使用的数据库驱动:JSQL Driver(JD
- 高级语言不能直接被机器所理解执行,所以都需要一个翻译的阶段,解释型语言用到的是解释器,编译型语言用到的是编译器。编译型语言通常的执行过程是:
- 引言事情是这样的,直接开讲面试官:npm run xxx的时候,发生了什么?讲的越详细越好。我(心想,简单啊): 首先,DNS 解析,将域名
- 1. 为什么要有转义?ASCII 表中一共有 128 个字符。这里面有我们非常熟悉的字母、数字、标点符号,这些都可以从我们的键盘中输出。除此
- 1. import_table介绍上期技术分享我们介绍了MySQL Load Data的4种常用的方法将文本数据导入到MySQL,这一期我们
- 支持两种用法:(1)合并某一文件夹下的所有文件(忽略文件夹等非文件条目)(2)显示的合并多文件。import sysimport os
- SQLite是一种嵌入式数据库,它的数据库就是一个文件。由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在
- 客户端从服务端下载文件的流程分析: 浏览器发送一个请求,请求访问服务器中的某个网页(如:down.php),该网页的代码如下。 服务器接受到
- 时间序列预测时间序列是按照时间顺序排列的数据集合,在很多应用中都非常常见。时间序列分析是对这些数据进行分析和预测的过程。时间序列预测是该分析
- 本文主要介绍的是关于Python利用requests模块下载图片的相关,下面话不多说了,来一起看看详细的介绍吧MySQL中事先保存好爬取到的
- 可能是因为编译太简单了,golang 并没有一个官方的构建工具(类似于 java 的 maven 和 gradle之类的),但是除了编译,我
- 本文实例讲述了Python利用正则表达式匹配并截取指定子串及去重的方法。分享给大家供大家参考。具体如下:import repattern=r
- 使用pyttsx的python包,你可以将文本转换为语音。安装命令pip install pyttsx3 -i https://pypi.t
- 前言将字符串动态转换为DOM节点,在开发中经常遇到,尤其在模板引擎中更是不可或缺的技术。字符串转换为DOM节点本身并不难,本篇文章主要涉
- 本来是只用Tenorflow的,但是因为TF有些Numpy特性并不支持,比如对数组使用列表进行切片,所以只能转战Pytorch了(pytor
- Golang 高并发问题的解决Golang在高并发问题上,由于协程的使用,相对于其他编程语言,已经有了很大的优势,即相同的配置上,Golan
- 前言Go 1.3 的sync包中加入一个新特性:Pool。这个类设计的目的是用来保存和复用临时对象,以减少内存分配,降低CG压力。type
- 写在前面的话:此篇还是asp相关的,相信玩ASP的都有这个感觉,当数据有5万多条时-------just like音乐网,要调用最新的10条
- 前三篇文章中,明确了栅格系统的设计细节和适用范围。这一篇将集中讨论960栅格系统的技术实现。Blueprint的实现Blueprint是一个