Recently, I'm studying how to make various types of robots with Python. Today, let's share a robot that automatically publishes Sina Weibo.
Basic ideas
In fact, it is not difficult to realize a simple robot for automatically publishing microblogs. It only needs to find the materials to be published on time every day (usually crawlers), and then publish them automatically through the API interface opened by microblog.
source material
For the material, I choose Kingsoft Ciba's daily English. We can crawl the relevant information of the website to form our microblog material.
http://news.iciba.com/views/dailysentence/
Microblog API
Microblog provides an open platform for developers
https://open.weibo.com/
Of course, we still need to register and create applications before using this platform. For example, like me, create an application
Then we also need to set the security domain name (which will be used when publishing microblogs later) in application information - > basic information.
You also need to set the application callback address in application information - > advanced information.
Of course, for the call of microblog API, we can use the third-party library encapsulated by others for more convenient operation. I use a library called "weibo" here and install it directly using pip.
pip install weibo
However, I encountered an installation problem. The error is as follows
Traceback (most recent call last): File "setup.py", line 15, in <module> long_description=open('README.rst').read(), UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 241: illegal multibyte sequence
It seems to be a coding problem. We can avoid it by modifying the source code
-
1. From pypi Download the Weibo library from www.weibo.org
-
2. Then unzip the package and modify the file setup Py the following code
long_description=open('README.rst', encoding='utf-8').read(),
-
3. Execute the following commands to install
python setup.py build python setup.py install
At this point, weibo library can be installed successfully
These are the preparation and writing ideas. Let's take a look at the specific process and code
Automatically publish microblog
According to the description document of weibo library, it is easy to use weibo library to operate weibo. We only need to provide the App Key, App Secret and application callback address information when we create weibo application. The simple calling code is as follows
from weibo import Client c = Client('App Key', 'App Secret', 'https://www.luobodazahui.top', username='username', password='password') c.post('statuses/share', status='haha' + Secure domain name)
We run the code. If nothing happens, we can see the microblog just released in the microblog under the account.
The simplest microblog has been published successfully. Can we publish microblogs with pictures? Let's try it
According to the document description of the interface statuses/share, we can pass in the pic parameter to upload pictures
Let's pass in the pic parameter and try again
f = open('4.png', 'rb') c.post('statuses/share', status='haha' + Secure domain name, pic=f) f.close()
Yes, it can publish microblogs with pictures normally
Now that the automatic microblog release is done, let's take a look at how to automatically obtain microblog materials
Microblog material
For the daily sentence of Kingsoft Ciba, we can use the following API to obtain daily information
http://open.iciba.com/dsapi/
You can see the following information returned by the interface
We can call this interface directly using the requests library
import requests url = 'http://open.iciba.com/dsapi/' res = requests.get(url) content_e = res.json()['content'] content_c = res.json()['note'] content = content_e + '\n' + content_c content_t = res.json()['picture2']
Now let's encapsulate the two functions to improve the function
def weibo(content, picture): ff = requests.get(picture).content c = Client(app_key, app_secret, 'https://www.luobodazahui.top', username=username, password=password) c.post('statuses/share', status=content + 'https://www.luobodazahui.com', pic=ff) def auto_weibo(): url = 'http://open.iciba.com/dsapi/' res = requests.get(url) content_e = res.json()['content'] content_c = res.json()['note'] content = content_e + '\n' + content_c content_t = res.json()['picture2'] weibo(content, content_t) return 'OK'
Now we can create a scheduled task and run the script at 7 o'clock every day
0 7 * * * python auto_weibo.py
The final effect is as follows
That's all for today's sharing. If you think it's useful, please be sure to point out a praise and support.