欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用新浪微博api上傳圖片到微博示例

 更新時間:2014年01月10日 09:44:07   作者:  
本文介紹了Python使用新浪微博官方api發(fā)表帶圖的微博(模擬post)的功能,大家參考使用吧

復(fù)制代碼 代碼如下:

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_()

相關(guān)文章

  • Python把對應(yīng)格式的csv文件轉(zhuǎ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è)置方式

    這篇文章主要介紹了基于tkinter中ttk控件的width-height設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • idea創(chuàng)建springMVC框架和配置小文件的教程圖解

    idea創(chuàng)建springMVC框架和配置小文件的教程圖解

    本文通過圖文并茂的形式給大家介紹了idea創(chuàng)建springMVC框架和配置小文件的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-09-09
  • 使用Python的pygame庫繪制圖形示例詳解

    使用Python的pygame庫繪制圖形示例詳解

    這篇文章主要介紹了使用Python的Pygame庫繪制圖形的方法,Pygame是被設(shè)計用來寫游戲的python模塊集合,Pygame是在優(yōu)秀的SDL庫之上開發(fā)的功能性包,通常使用Pygame來開發(fā)具有全部特性的游戲和多媒體軟件,感興趣的朋友可以參考下
    2024-02-02
  • PyQt Qt Designer工具的布局管理詳解

    PyQt Qt Designer工具的布局管理詳解

    這篇文章主要介紹了PyQt Qt Designer工具的布局管理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Python正則表達(dá)式非貪婪、多行匹配功能示例

    Python正則表達(dá)式非貪婪、多行匹配功能示例

    這篇文章主要介紹了Python正則表達(dá)式非貪婪、多行匹配功能,結(jié)合實例形式分析了Python正則表達(dá)式中非貪婪及多行匹配功能的實現(xiàn)方法與相關(guān)注意事項,需要的朋友可以參考下
    2017-08-08
  • pymysql模塊的操作實例

    pymysql模塊的操作實例

    在本篇文章里小編給大家分享的是關(guān)于pymysql模塊的簡單操作,有需要的朋友們可以參考下。
    2019-12-12
  • 詳解Windows下源碼編譯PaddlePaddle

    詳解Windows下源碼編譯PaddlePaddle

    這篇文章主要為大家介紹了Windows下從源碼編譯PaddlePaddle解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 如何使用pytorch實現(xiàn)LocallyConnected1D

    如何使用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-09
  • TorchVision Transforms API目標(biāo)檢測實例語義分割視頻類

    TorchVision Transforms API目標(biāo)檢測實例語義分割視頻類

    這篇文章主要為大家介紹了TorchVision Transforms API大升級,支持目標(biāo)檢測、實例/語義分割及視頻類任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11

最新評論