python使用新浪微博api上傳圖片到微博示例
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('發(fā)表')
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_()
- 利用Python爬取微博數(shù)據(jù)生成詞云圖片實例代碼
- python繞過圖片滑動驗證碼實現(xiàn)爬取PTA所有題目功能 附源碼
- Python 爬蟲批量爬取網(wǎng)頁圖片保存到本地的實現(xiàn)代碼
- 利用python批量爬取百度任意類別的圖片的實現(xiàn)方法
- Python使用xpath實現(xiàn)圖片爬取
- Python Scrapy圖片爬取原理及代碼實例
- python3 requests庫實現(xiàn)多圖片爬取教程
- Python3直接爬取圖片URL并保存示例
- python實現(xiàn)爬取百度圖片的方法示例
- python3 爬取圖片的實例代碼
- Python使用爬蟲爬取靜態(tài)網(wǎng)頁圖片的方法詳解
- python制作微博圖片爬取工具
相關(guān)文章
Python把對應(yīng)格式的csv文件轉(zhuǎn)換成字典類型存儲腳本的方法
今天小編就為大家分享一篇Python把對應(yīng)格式的csv文件轉(zhuǎn)換成字典類型存儲腳本的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02基于tkinter中ttk控件的width-height設(shè)置方式
這篇文章主要介紹了基于tkinter中ttk控件的width-height設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05idea創(chuàng)建springMVC框架和配置小文件的教程圖解
本文通過圖文并茂的形式給大家介紹了idea創(chuàng)建springMVC框架和配置小文件的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-09-09如何使用pytorch實現(xiàn)LocallyConnected1D
由于LocallyConnected1D是Keras中的函數(shù),為了用pytorch實現(xiàn)LocallyConnected1D并在960×33的數(shù)據(jù)集上進(jìn)行訓(xùn)練和驗證,本文分步驟給大家介紹如何使用pytorch實現(xiàn)LocallyConnected1D,感興趣的朋友一起看看吧2023-09-09TorchVision Transforms API目標(biāo)檢測實例語義分割視頻類
這篇文章主要為大家介紹了TorchVision Transforms API大升級,支持目標(biāo)檢測、實例/語義分割及視頻類任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11