文章正文

Python-天猫商品上下架预警

【文章】2020-04-23

简介Python-天猫商品上下架预警

Python抓取示例,仅供参考:

import requests
import hashlib
import time


def get_url(url):
    headers = {
        "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 "
                      "(KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
        'Connection': 'close'
    }
    result = requests.get(url, headers).text
    return result


def goods_off(url):
    content = get_url(url)
    return '此商品已下架' in content


def send_msg_ding(msg_content):
    title = '店铺商品下架预警'
    dingurl = 'https://oapi.dingtalk.com/robot/send?access_token=9ebdf461fc68bef556772f277f2c0941f0164046d940222347e2ee7733fee0c7'
    sign = md5('collect' + time.strftime("%Y-%m-%d", time.localtime()))
    send_url = 'http://tapi.manyic.com/pChargeApi/notice/collect?title={}&content={}&receiveType={}&receiver={}&sign={}'.format(
        title, msg_content, 'DINGGROUP', dingurl, sign)
    result = get_url(send_url)
    print("send_msg_result:" + result)


def send_msg_web(msg_content):
    title = '店铺商品下架预警'
    sign = md5('collect' + time.strftime("%Y-%m-%d", time.localtime()))
    send_url = 'http://tapi.manyic.com/pChargeApi/notice/collect?title={}&content={}&receiveType={}&receiver={}&sign={}'.format(
        title, msg_content, 'WEB', 'ALL', sign)
    result = get_url(send_url)
    print("send_msg_result:" + result)


def md5(data):
    hash = hashlib.md5(data.encode('utf-8'))
    return hash.hexdigest()


path = r"goods.txt"
f = open(path, "r", encoding='utf-8')
msg_content = ''
while True:
    time.sleep(5)
    for line in f.readlines():
        lines = line.split(",")
        if len(lines) == 2:
            time.sleep(3)
            if goods_off(lines[1]):
                msg_content = '{}@_ET_@{}'.format(msg_content, lines[0])
if len(msg_content) > 1:
    send_msg_ding(msg_content)
    send_msg_web(msg_content)
if f:
    f.close()

打赏支持

感谢您的支持,加油!

打开微信扫码打赏,你说多少就多少

找书费时,联系客服快速获取!

扫码支持

在线客服8:30-22:30,若离线请留言!

获取教程,请联系在线客服!

扫码支持

在线客服8:30-22:30,若离线请留言!

热门阅读

找PDF电子书,太费时间?

  • 微信扫描二维码,让客服快速查找。
  • 在线客服8:30-22:00,若离线请留言!