The robot has been writing for so long that it hasn’t had time to document its development (until ORZ is lazy). The following uses my name for the robot, Chowcool, as the main body. All right, jerk off.

First of all, I will briefly introduce the birth history of Chowcool. In line with the principle of saving money for women on Taobao (and earning some gross ticket for myself), I will write an automatic recognition of whether there is a rebate for taobao items and send a rebate link. Then the wechat robot with social attributes as the basic law emerged.

But Too young Too naive, people’s needs are endless. During this period, a series of daily necessities such as identifying car models by looking at pictures and detecting appearance level by looking at pictures are added. Anyway, Chowcool is stable now.

Let’s analyze the entire robot production process:

  1. Work is good, so first prepare the tools, I chose the development environment:
Compiler environment: Python3 editor: Sublime OS: MacOX Python Third-party lib: Requests, wxpy built-in Lib: : OS, Base64, Hashlib WebAPI: Web search face detection, vehicle recognition, etcCopy the code

  1. Next, let’s talk about the design idea of Chowcool. First, let’s talk about the application scenario of this function
The user has confirmed that he has the intention to purchase the article, and then queries whether this article has the link of rebate. So first we make a sort of input, the possible input situation is as followsCopy the code
1. Enter taobao share link, there are two kinds of links, one is short link, the other is original link. However, the short link is different, need to do different processing 2. Input item name or keyword, here need to pay attention to is to add illegal character verification, as well as the failure of the prompt, relatively simpleCopy the code

Next, confirm what the output information to the user includes. Here I give it directly, and you can adjust it according to your personal needs:

No.5 children 🎉 basketball 🎉 Genuine soft leather primary school students indoor and outdoor training practice super wear resistant blue ball special monthly sale :19 Original price :198.0¥discounted price :85.0¥Can get rebate :42.5¥ Link: http://item.taobao.com/item.htm?id=10722793500Copy the code

Finally, the requirements are clear and you can start writing the processing logic, filling in the holes as you put the code in.


  1. First of all, we need to create a new wechat robot, which is so easy because we have GitHub, which has several packaged lib. Finally, we find that WXpy is easier to write.
from wxpy import *
bot = Bot(qr_path='/Users/funny/Desktop/qr.png', cache_path=True)
@bot.register(except_self=False)
def reply_auto(msg):# this is the message listener function
print(msg.text)
Copy the code

The robot is so simple to build, and then define a message listening function, you can get the message sent by the user, see the wXpy documentation, the address is not posted

We need to find some rebate networks to obtain data. We can only obtain the data we want by analyzing the web protocol, which is equivalent to copying an API. Here, I use Taobao’s own rebate marketing platform, and then build the data acquisition process through analysis:

url = 'https://pub.alimama.com/items/search.json'
page = 1
size = 50
header = {
           'accept': 'application/json, text/javascript, */*; Q = 0.01 '.'accept-encoding': 'gzip, deflate, br'.'accept-language': 'zh-CN,zh; Q = 0.9, en. Q = 0.8 '.'Host': 'pub.alimama.com'.'referer': 'http://pub.alimama.com/promo/search/index.htm?>q=%E8%B7%AF%E7%94%B1&_t=1527140719231'.'Cookie': 't=65a3f118f0d5d43c7fe117a8be08f817; cookie2=13fd1221dec3ea9763637418957b893b; v=0; _tb_token_=33be65ded35eb; cna=SS6EEzNgDwsCAd73tCd2fBcM; undefined_yxjh-filter-1=true; alimamapwag=TW96aWxsYS81LjAgKE1hY2ludG9zaDsgSW50ZWwgTWFjIE9TIFggMTBfMTNfNCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBH ZWNrbykgQ2hyb21lLzY2LjAuMzM1OS4xODEgU2FmYXJpLzUzNy4zNg%3D%3D; cookie32=f3baeec7c052461cbe5eb16eee1db478; alimamapw=Rw8HDVJeAQlDDVFYXF5cV19YPQcBUVJQUQZRCQMMBw4DVVQJVwQDBlUEAQZXX1UIVFJT; cookie31=MTMyNDQwMDk1LHM4Mzg0ODE4JTQwYWxpbWFtYSwxMTczNTkwNjJAYWxpbWFtYS5jb20sVEI%3D; login=U%2BGCWk%2F75gdr5Q%3D%3D; taokeisb2c=; account-path-guide-s1=true; rurl=aHR0cDovL3B1Yi5hbGltYW1hLmNvbS8%2Fc3BtPWEyMTl0Ljc2NjQ1NTQuYTIxNHRyOC45LjJlMjAzNWQ5dFdKVE44; 132440095_yxjh-filter-1=true; apushfab5c63114239d725bd0105e1b8e4eaa=%7B%22ts%22%3A1527140884215%2C%22heir%22%3A1527140728008%2C%22parentId%22%3A152714 0709470%7D; isg=BEZGCQKXYVfeTzWwL8e04rPtlzoID4tlcvwS2zBrhGlVM_BNhTZNc6eFDm__v4J5'.'user-agent': 'the Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'.'x-requested-with': 'XMLHttpRequest'}
content = msg.text
preload = {'q': content,# Here's what to search for
              '_t': '1527074885336'.'toPage': page,
              'quseryType': '0'.'sortType': '1'.'auctionTag': ' '.'perPageSize': size,
              'shopTag': ' '.'t': '1527078218120'.'_tb_token_': '33be65ded35eb'.'pvid': '10. _222 240.118.0 _745_1527078217613'}
   # print(preload)
taobao = requests.get(url=url, params=preload)
taobao = json.loads(taobao.text)
Copy the code

This whole section is the protocol for retrieving data, where content is the input we define. This input is not directly usable, and the following processing needs to be done:

def shorturlRe(url):# Here is short chain to long chain
   req = requests.get(url=url)
   html = etree.HTML(req.text)
   return html.xpath('//script')[1].xpath('text()')[0].split('var')[3].split(";")[0].split("'") [1]if msg.sender not in mp:
   if content.startswith('【') :# print(len(content.split(' ')))
     if len(content.split(' ')) > 5:
       content = content.split(' ')[0].split('】')[1]
       content = shorturlRe(content)
     else:
         content = content.split('】')[0].split('【') [1]Copy the code

This code is common message processing and basically does not need to be changed. Note that you need to define a short chain to a long chain function, which can be handled as needed.

Now that we’re done with the hard part, we just have to wait for the data to come back, process the data output, and it’s going to return JSON, and it’s going to turn into a dictionary.

title = taobao['data'] ['pageList'] [0] ['title'].replace('<span class=H>'.'🎉').replace('</span>'.'🎉')
     # print(title)
     msale = taobao['data'] ['pageList'] [0] ['biz30day']

      reservePrice = taobao['data'] ['pageList'] [0] ['reservePrice']
      zkPrice = taobao['data'] ['pageList'] [0] ['zkPrice']
      returnFee = taobao['data'] ['pageList'] [0] ['tkCommFee']
      img = 'https:{}'.format(taobao['data'] ['pageList'] [0] ['pictUrl'])
      img = requests.get(img).content
      with open('/users/funny/desktop/1.jpg'.'wb') as f:
        f.write(img)
      link = taobao['data'] ['pageList']
Copy the code

Finally, the data is returned to the user, and attention should be paid to the case of abnormal data return. The processing can be divided into two cases when the specified item is not found and the recommended item is returned, and the input is abnormal and the re-input is prompted:

if not link or not content:
       if msg.sender == people:
         msg.reply_msg('Unable to find corresponding item, please re-enter')
       else:
         msg.reply_msg(text)
     else:
       msg.reply('{}\n month sale :{}\n original price :{}¥n discounted price :{}¥n Can get rebate :{}¥n link :{}'.format(
           title, msale, reservePrice, zkPrice, returnFee, link[0]['auctionUrl']))
       if 'darwin' in sys.platform:
         # print(sys.platform)
         msg.reply_image('/users/funny/desktop/1.jpg')
         os.remove('/users/funny/desktop/1.jpg')
         # print('done')
       else:
         msg.reply_image(os.path.abspath(os.path.curdir) + '\ \' + '1,jpg')
         os.remove(os.path.abspath(__file__) + '\ \' + '1,jpg')
   except TypeError as e:
     # print(people)
     if msg.sender == people:
       msg.reply_msg('Unable to find corresponding item, please re-enter')
     else:
       msg.reply_msg(text)
Copy the code

This article does not include the code for car identification and appearance detection based on the image. In fact, the principle is the same. The complete code for the above functions will be put here on GitHub.


Write these articles is to just start the fuel injection do some reference, welcome point star crazy praise, incidentally hit an advertisement, appearance level calculator small procedures, source see here