程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python full stack to create a Baoke WeChat robot

編輯:Python

本篇 Chat 將會涉及:

Python Environment of a treasure guest API 的使用;

使用 Python 第三方模塊 itchat Implement a letter the automatic reply of the login operation;

PyQt5 The layout of the management and multi-threaded applications

  1. 前言
  2. What is a treasure to guest

相信很多人都聽過或者接觸過各類導購 APP、QQ 群、WeChat group share a treasure goods discount coupons or other information.


The user can get on these channels compared with goods from one platform to search out treasure more preferential price or get larger denomination coupons.

這些渠道,Is a treasure of the channel.

A guest is a treasure of platform to launch a charged according to clinch a deal the promotion mode,

通常也指通過這種模式賺錢的人.

A few years ago when a guest red treasure,

各類導購網站、導購 APP 以及優惠券 QQ 群、微信群層出不窮,如今倒是稍顯平淡了.

Although a guest in the flock of the Internet is a treasure,

但是絲毫影響不了新加入的人群的心態,

In a certain outfit in the search “Buy one letter machine”,

Can also see a lot of businesses by selling a treasure of a robot:


These robots a letter but mainly are the functions of automatic reply within a group of people the message,And timed push some preferential commodity information, etc.

今天,We will use which will appear,Either which boa constrictors——Python,To build a robot a treasure guest a letter,Implementation in the group chat auto reply search and return goods this guide a treasure to the guest the most basic function of WeChat robot.

Registered as a treasure to guest

To write a letter a treasure of a robot,

We first need to become a a treasure guest,

To obtain a treasure guest related preferential resource.

方法很簡單,

In a treasure guest promotion platform——almmA treasure(A treasure·生態伙伴)A register an account you can,

We can use a treasure account to log in.

在登錄之後,我們可以在 “My league” 中的 “推廣管理” In the promotion of media Settings:

在其中,We can add site、APP、導購、Software promotion, etc.

在此,I have added a website media,And has been submitted successfully verified.

Promotion in the media is a treasure to the guest a basic channels of promotion goods,Used to belong to different marketing a,If there is no promotion media,There was no way to create promotion a,Which can't be a treasure to the guest promotion.

We can according to their own situation to create promotion media,It is recommended to use website promotion media.

After create good promotion media,We also need to create a promotion,

A one treasure guest used to belong to a specific promotion goods,

Here I have created the web site to promote a,

其中的 PID Behind our call API Interface will use to,大家注意一下:


If there is no promotion of a,那麼我們可以在 “Union products” 中點擊 “精選清單” Download button immediately:


It will let us choose to promote a,This can create a new promotion a:

So that we will get a promotion website to promote a.

Back to promote media management,

In the media we add website throughalmmAfter the success of the validation of the,

We're going to get a website alliance cooperation API:

點擊 “查看” 鏈接,

Will jump to an open platform we create web site to promote a(To promote displayed on the ali open platform for an application)An overview of,

Which shows our application interface information,

Data interface and we get permission API 等:

APP 證書中的 App Key 和 App Secret 會在調用 api Interface as credentials to authenticate,Below the function of the scene showed that we obtained API 接口:The application of here I have:

  • A treasure customer base API
  • A treasure to the guest - 媒體 - 內容消息
  • A password based package
  • A treasure to the guest - 工具 - 超級搜索

Some treasure customer base API Contains some of the other interfaces,We mainly use the API 包.

這樣,我們就可以開始通過 API Interface to a treasure promotion information.

Python Environment using a treasure guest API

In the register as taobao guest,Add to promote access to the basis of a treasure guest API 之後,We can by calling for a treasure API To get the taobao guest promotion information.

Understand some treasure customer interface

In the call for a treasure API 之前,我們先來看一下,

After submit websites promoting obtain a treasure customer base API What are in the interface:


其中一共有 23 個接口,

Although it is a basis API 包,

But contains a treasure most of guest promotion commodity information to obtain.

在本篇 Chat Create a treasure guest WeChat robot,

We mainly use the following two interface:

清單 API【導購】taobao.tbk.dg.item.coupon.get
Guest a password taobao.tbk.tpwd.create
其他的 api Interface you can according to your own need to add in the program.

清單 API Commodity coupons for returning a treasure information,We can use it to search for specific keywords of commodity coupons,As the results returned to the user.The acceptable parameters as follows:

A guest tao treasure password API,Used to transform a treasure guest promotion link for the new password,In order to realize the replication after coupons,Open a treasure you can jump to promote the effect of the link.The acceptable parameters as follows:

可以發現,

我們主要使用 adzoneid、A password bounced content and link to a password to jump a goal three parameters,

其中 adzoneid From the promotion of PID 中獲取,

A password bounced content can be set to the title of the goods,

Link is a password to jump a goal coupon link address,

These two parameters can be from a good coupon listing API 中獲取到.

Let's look at how to call this two interfaces.

Call taobao guest API 接口

在 API The introduction of the interface,

We can see that there are multiple languages request instance,如下所示:


最幸運的是,其提供了 Python 的請求示例,

So that we can modify a little bit about the sample code(示例中為 Python2 版本的代碼)A treasure to guest API 進行調用了,

是不是很美好?

雖然看起來很美好,But we still need to do some extra work.

下載 al 提供的 SDK

See the sample code in the first 2 行引入了一個 top.api 的模塊,

This module is belong toal開放平台提供的 SDK,

使用 pip 等 Python Package management tools can not download and install.

在alEach application development platform,

都提供了相應的 SDK 的下載鏈接,

我們選擇 “Python 版本”,In the click to download.


下載完成的 SDK 是一個名為 “sdk-python-*” 的壓縮包,Including a package and an empty folder:

我們需要的 top 模塊就在 taobao-sdk-PYTHON-auto The compression package,繼續解壓,得到以下文件:

In the face of a strange package,Good habits, of course, is to look at readme Such as files to know to use

我們打開 READ_ME.TXT 文件,The inside of the content is not much,

But the top 模塊如何使用:

復制 top Folder to own Python 項目路徑下,

讓 Python Actuator can introduce this top 包即可.

Got a treasure guest SDK The use of the module and module after,

It seems we can leave for.

如果你和我一樣,使用的是 Python 3 的話,

You can still need to wait.

因為alOpen platform provided by this SDK 是 Python 2 版本的,

直接在 Python 3 環境下調用會報錯,

But the main position error are located in top 文件夾的 api 文件夾下的 base.py 這個文件:


We need to code style and specification,

可以使用 Python 官方提供的 2to3.py 這個工具.

在這裡,I have convert it to modify well and uploaded to the baidu network location,

Students in need can be downloaded to replace,

鏈接為:
base.py_免費高速下載|百度網盤-分享無限制

這一步完成之後,Our preparation work is over.下面使用 Python To invoke a certain treasure customer interface.

Call a treasure guest API

當前,Our project directory as follows:


In the project under the main folder,There is only one for a treasure top 包,

我們新建一個 Python 文件,Used for debugging calls for a treasure API:


首先在 usetbkapi.py File into a treasure to the guest's bag——top,

Good and define public parameters(App Key 和 App Secret,

兩者從al開放平台 - The overview of application in)And good coupon listing need adzoneid(A treasure - To promote a management - Promote a PID 中 mmxxxxxxxxx 的第三位):

import top.api
appkey = '你自己的appkey'
secret = '你自己的appsecret'
adzone_id = Promote aadzone_id

First is good coupon listing API,

In the open platform ali API 文檔中,The sample request as follows:

# -*- coding: utf-8 -*-
import top.api
req=top.api.TbkDgItemCouponGetRequest(url,port)
req.set_app_info(top.appinfo(appkey,secret))
req.adzone_id=123
req.platform=1
req.cat="16,18"
req.page_size=1
req.q="女裝"
req.page_no=1
try:
resp= req.getResponse()
print(resp)
except Exception,e:
print(e)

We will be change,變成了如下形式:

# For taobao passenger coupon
def get_tbk_coupon(keyword):
req = top.api.TbkDgItemCouponGetRequest()
req.set_app_info(top.appinfo(appkey, secret))
req.adzone_id = adzone_id
# Goods platform:1為PC端,2For the wireless side,默認為1
req.platform = 2
# 商品的類目ID
req.cat = "16,18"
# Returns the number of each page
req.page_size = 5
# Commodity search term
req.q = keyword
# The number of pages of the returned goods
req.page_no = 1
try:
resp = req.getResponse()
print(resp)
except Exception as e:
print(e)

函數 gettbkcoupon() Accept a parameter as the taobao guest goods coupon search words,We run the function,以 “Python” As a result of the search words to see back:

if __name__ == '__main__':
get_tbk_coupon('python')

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved