主頁 > 軟體設計 > 如何用python腳本獲取和回復阿里國際站的新詢盤和未讀TM資訊,并轉發到微信

如何用python腳本獲取和回復阿里國際站的新詢盤和未讀TM資訊,并轉發到微信

2020-10-08 16:00:26 軟體設計

阿里國際站地址:阿里國際站

專案背景:

用python腳本監控阿里國際站的未讀詢盤和TM資訊,并轉發到微信,可以提醒業務員回復,在晚上睡覺期間實作自動回復,可以提高回復及時率,做好客服,提升店鋪競爭力,

我們可以把實作思路分為以下幾步:

第一步:用python腳本呼叫微信發資訊,
第二步:首先在瀏覽器訪問相應的網頁,寫幾個js腳本,實作登錄阿里國際站,獲取未讀詢盤和回復新詢盤,獲取和回復未讀TM資訊,
第三步:把各個js腳本都單獨另存為一個文本,用python腳本打開瀏覽器,訪問網頁以及運行相應的js腳本,
第四步:將獨立的功能整合起來,實作完整的功能,

python發微信資訊

一開始試了itchat,發現我的微信號被限制登錄網頁版微信,實作不了,后來找到了python呼叫微信電腦版發資訊的WechatPCAPI專案,就直接拿下來用了,
運行環境要求為:python 3.7.4、微信電腦版 2.7.1.82,python 3.7.4下載地址:python 3.7.4,微信電腦版 2.7.1.82下載地址:微信電腦版 2.7.1.82,提取碼: nidd,WechatPCAPI使用方法參考這里:WechatPCAPI,

js腳本操作阿里國際站網頁

我把登錄阿里國際站js腳本拆成了上中下3個部分,中間部分是各個賬號的賬號和密碼,執行腳本的時候再全部合并回來,

login_top.txt

/*
登錄阿里后臺
https://passport.alibaba.com/icbu_login.htm
*/

function login() {
	var login_id = document.getElementById("fm-login-id"); //賬號輸入框
	var login_password = document.getElementById("fm-login-password"); //密碼輸入框
	login_id.value =

login_bottom.txt

    var login_submit = document.getElementById("fm-login-submit"); //登錄按鈕
	login_submit.click(); //點擊登錄

	return "登錄成功"
}

return login();

獲取未讀詢盤資料串列:

inquiry_list.txt

/*
獲取未讀詢盤資料串列
https://message.alibaba.com/message/default.htm?spm=a2700.7756200.0.0.5d3c71d2vw5Viy#feedback/all
*/

function inquiry_list() {

	inquiry_area = ''; //詢盤資料區域
	var div = document.getElementsByTagName("div");
	for (var i = 0; i < div.length; i++) {
		if (div[i].className == "aui-grid-container") {
			inquiry_area = div[i];
			break;
		}
	}

	var inquiry_div = inquiry_area.getElementsByTagName("div");
	unread_inquiry = []; // 未讀詢盤資訊

	for (var i = 0; i < inquiry_div.length; i++) {
		if (inquiry_div[i].className == "aui2-grid-wraper actions-view-") {

			inquiry_json = {}; // 單條詢盤資訊

			var inquiry_url = inquiry_div[i].getElementsByTagName("a")[0];
			//console.log(inquiry_url.href);
			if (inquiry_url != undefined) {
				inquiry_json["url"] = inquiry_url.href; //詢盤鏈接
			}

			var inquiry_div2 = inquiry_div[i].getElementsByTagName("div");
			for (var j = 0; j < inquiry_div2.length; j++) {
				if (inquiry_div2[j].className == "util-left m-header-padding spec-inquiry-id util-ellipsis") {
					//console.log(inquiry_div2[j].innerText);
					inquiry_json["id"] = inquiry_div2[j].innerText; // 詢價單號
				}
				if (inquiry_div2[j].className == "util-left m-header-padding") {
					//console.log(inquiry_div2[j].innerText);
					inquiry_json["time"] = inquiry_div2[j].innerText // 時間
				}
				if (inquiry_div2[j].className == "aui2-grid-name u-text-center") {
					//console.log(inquiry_div2[j].innerText);
					inquiry_json["sender"] = inquiry_div2[j].innerText; // 發起人
				}
				if (inquiry_div2[j].className == "aui-grid-inner-text aui-grid-owner-name") {
					//console.log(inquiry_div2[j].innerText);
					inquiry_json["owner"] = inquiry_div2[j].innerText; // 負責人
				}
			}

			var inquiry_tr = inquiry_div[i].getElementsByTagName("tr");
			for (var j = 0; j < inquiry_tr.length; j++) {
				if (inquiry_tr[j].className.trim() == "has-unread") {
					//console.log("unread");
					inquiry_json["read"] = "unread"; // 未讀
				}
			}

			var inquiry_td = inquiry_div[i].getElementsByTagName("td");
			for (var j = 0; j < inquiry_td.length; j++) {
				if (inquiry_td[j].className.trim() == "aui2-grid-quo-status-col") {
					//console.log(inquiry_td[j].innerText);
					inquiry_json["status"] = inquiry_td[j].innerText.trim().replace(/\s+/g, "   "); // 狀態
				}
				if (inquiry_td[j].className.trim() == "buyer-list-actions") {
					//console.log(inquiry_td[j].innerText);
					inquiry_json["operation"] = inquiry_td[j].innerText; // 操作
				}
			}

			var inquiry_p = inquiry_div[i].getElementsByTagName("p");
			for (var j = 0; j < inquiry_p.length; j++) {
				if (inquiry_p[j].className == "buyer-chat-msg") {
					//console.log(inquiry_p[j].innerText);
					inquiry_json["chat"] = inquiry_p[j].innerText;
				}
			}

			//console.log(inquiry_json);
			if (inquiry_json["read"] != undefined) {
				unread_inquiry.push(inquiry_json);
			}

		}
	}

	//console.log(unread_inquiry);
	return unread_inquiry;

}

return inquiry_list();

回復新詢盤:

inq_rep_send.txt

function inquiry_reply_send() { //回復新詢盤
	var input_holder = document.getElementsByClassName("holder")[0]; //輸入開關
	//console.log(input_holder.innerText);
	input_holder.click();
	setTimeout(function() {
		var send_confirm = document.getElementById("dialog-footer-2"); //代人回復按鈕區域
		//console.log(send_confirm.innerHTML);
		if (send_confirm != undefined) {
			var send_confirm_button = send_confirm.getElementsByClassName("next-btn next-btn-primary next-btn-medium")[0]; //代人回復按鈕
			//console.log(send_confirm_button.innerText);
			send_confirm_button.click();
		}
		var input_area = document.getElementById("normal-im-send_ifr").contentWindow.document; //輸入內容區域
		var input = input_area.getElementsByTagName("p")[0]; //輸入內容標簽
		var input_text = "Dear Customer,\n\n" +
			"Good day!\n" +
			"We're looking forward to your arrival.\n\n" +
			"Please feel free to leave a message to tell us more about your requests or contact information.\n" +
			"We'll reply you as soon as possible.\n\n" +
			"Thanks & Best regards"; //輸入的內容
		input.innerText = input_text;
		setTimeout(function() {
			var bold_icon = document.getElementsByClassName("mce-ico mce-i-bold")[0]; //點擊加粗字體圖表激活發送內容
			bold_icon.click();
			var send_button = document.getElementsByClassName("send")[0]; //發送按鈕
			//console.log(send_button.innerText);
			send_button.click();
		}, 2000)
	}, 2000)
}

return inquiry_reply_send();

檢查回復新詢盤是否成功:

inq_rep_che.txt

function check_reply_send() { //檢查是否已自動回復
	var chat_content = document.getElementsByClassName("session-rich-content text"); //交談內容
	var send_flag = 0; //判斷是否已回復
	for (var i = 0; i < chat_content.length; i++) {
		if (chat_content[i].innerText.search(
				"Please feel free to leave a message to tell us more about your requests or contact information.") != -1) {
			send_flag = 1;
			break;
		}
	}
	if (send_flag == 1) {
		return "發送成功";
	} else {
		return "發送失敗";
	}
}

return check_reply_send();

獲取TM資訊資料串列:

tm_list.txt

/*
獲取TM資訊資料串列
https://www.alibaba.com/
*/

function tm_list() {

	var tm_iframe = document.getElementById("#weblite-iframe").contentWindow.document; //加載TM資訊
	//console.log(tm_iframe);

	var tm_area = tm_iframe.getElementById("icbu-weblite-list"); //TM資訊區域
	var unread_count = 0; //未讀TM資訊條數
	if (tm_area.getElementsByClassName("unread-count")[0] != undefined) {
		var unread_count = tm_area.getElementsByClassName("unread-count")[0].innerText;
	}
	//console.log(unread_count);
	var tm_list = tm_area.getElementsByClassName("recent-list")[0]; //TM最近資訊串列
	var per_tm_list = tm_list.getElementsByClassName("recent-list-item recent-list-item-unread"); //單個咨詢人TM資訊

	var all_tm = []; //全部TM資訊

	for (var i = 0; i < per_tm_list.length; i++) {

		per_tm = {}; //單條TM資訊

		//console.log(per_tm_list[i].outerHTML);
		var conversation_id = per_tm_list[i].outerHTML.split("conversationId&quot;:&quot;")[1].split("&quot;")[0]; //會話id
		//console.log(conversation_id);
		per_tm["conversation_id"] = conversation_id;
		var unread_num = per_tm_list[i].getElementsByClassName("unread-num")[0]; //未讀資訊數
		//console.log(unread_num.innerText);
		per_tm["unread_num"] = unread_num.innerText;
		var sender = per_tm_list[i].getElementsByClassName("name")[0]; //咨詢人
		//console.log(sender.innerText);
		per_tm["sender"] = sender.innerText;
		var contact_time = per_tm_list[i].getElementsByClassName("contact-time")[0]; //聯系時間
		//console.log(contact_time.innerText);
		per_tm["contact_time"] = contact_time.innerText;
		var latest_msg = per_tm_list[i].getElementsByClassName("latest-msg latest-msg-rec")[0]; //最新資訊
		if (latest_msg) {
			//console.log(latest_msg.innerText);
			per_tm["latest_msg"] = latest_msg.innerText;
		}

		//console.log(per_tm);
		all_tm.push(per_tm)
	}

	//console.log(all_tm);
	return all_tm;
}

return tm_list();

展開TM聊天框:

tm_open.txt

function open_tm() { //展開TM聊天框
	var tm_iframe = document.getElementById("#weblite-iframe").contentWindow.document; //獲取TM資訊區域
	var open_icon = tm_iframe.getElementsByClassName("next-icon next-icon-arrow-up-filling next-icon-xl fold-up")[0]; //展開TM圖示
	open_icon.click();
	return "展開TM聊天框成功";
}

return open_tm();

展開未讀聊天對話框:

tm_open_send.txt

function open_send(tm_id) { //點擊展開未讀聊天
	var tm_iframe = document.getElementById("#weblite-iframe").contentWindow.document; //獲取TM資訊區域
	var recent_list = tm_iframe.getElementsByClassName("recent-list")[0]; //全部TM資訊
	recent_list_item = recent_list.getElementsByClassName("recent-list-item recent-list-item-unread"); //單個咨詢人TM資訊;
	for (var i = 0; i < recent_list_item.length; i++) {
		//console.log(recent_list_item[i].outerHTML);
		var conversation_id = recent_list_item[i].outerHTML.split("conversationId&quot;:&quot;")[1].split("&quot;")[0];
		//console.log(conversation_id);
		if (conversation_id == tm_id) {
			recent_list_item[i].click();
			return "展開新TM對話框成功"
		}
	}
}

發送TM資訊:

tm_open_send.txt

function send_reply() { //發送TM資訊
	setTimeout(function() {
		var tm_iframe = document.getElementById("#weblite-iframe").contentWindow.document; //獲取TM資訊區域
		var message_box = tm_iframe.getElementsByClassName("message-box")[0]; //TM聊天框
		var send_textarea = message_box.getElementsByClassName("send-textarea")[0]; //TM聊天輸入框
		var send_msg = "Sorry, I'm not online now.\n" +
			"I'll reply you as soon as possible. \n" +
			"Thank you.";
		send_textarea.value = send_msg; //輸入回復內容
		setTimeout(function() {
			var send_button = message_box.getElementsByClassName(
				"next-btn next-btn-primary next-btn-medium send-tool-button")[0]; //發送按鈕
			send_button.click();
		}, 2000)
	}, 2000)
}

return send_reply();

檢查是否發送TM資訊成功:

tm_check_reply.txt

function check_reply() { //檢查是否已經自動回復
	var send_flag = 0; //判斷是否回復成功
	var tm_iframe = document.getElementById("#weblite-iframe").contentWindow.document; //獲取TM資訊區域
	var message_box = tm_iframe.getElementsByClassName("message-box")[0]; //TM聊天框
	var send_text = message_box.getElementsByClassName("session-rich-content text")[0]; //最新聊天內容;
	//console.log(send_text.innerText);
	if (send_text.innerText.search(
			"Sorry, I'm not online now.") != -1) {
		send_flag = 1;
	}
	if (send_flag == 1) {
		return "發送成功";
	} else {
		return "發送失敗";
	}
}

return check_reply();

最后是退出登錄,這個腳本可以忽略,24小時運行監控程式不需要退出登錄,

logout.txt

/*
阿里后臺退出登錄
https://www.alibaba.com/
*/

function logout() {
	logout_button = '' //退出按鈕
	var a = document.getElementsByTagName("a");
	for (var i = 0; i < a.length; i++) {
		if (a[i].innerText == "退出" || a[i].innerText == "Sign Out") {
			logout_button = a[i];
			logout_button.click() //點擊退出
			break;
		}
	}
	return "退出登錄成功"
}

return logout();

python腳本部分:

這里用到selenium的webdriver的chromedriver控制谷歌瀏覽器訪問網址,并執行自己寫的js腳本,chromedriver下載地址: chromedriver,
注意:要下載與安裝的谷歌瀏覽器相同版本的chromedriver,否則程式運行時會出錯,

ali_message.py

# -*- coding: utf-8 -*-
from WechatPCAPI import WechatPCAPI
import time
import logging
from queue import Queue
import threading
from time import sleep
from selenium import webdriver
import os

# 引入 chromedriver.exe
chromedriver = "C:/Program Files/Google/Chrome/Application/chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
# 阿里后臺登錄網址
login_url = 'https://passport.alibaba.com/icbu_login.htm'
# 未讀詢盤頁面網址
inquiry_url = 'https://message.alibaba.com/message/default.htm?spm=a2700.7756200.0.0.5d3c71d2vw5Viy#feedback/all'
# 獲取TM資訊頁面網址
tm_url = 'https://www.alibaba.com/'


# 加載js腳本
def load_js(ext_js):
    with open(ext_js, 'r', encoding='utf-8') as ext_js_file:
        js = ext_js_file.read()
        print('加載' + ext_js + '成功')
        return js


# 登錄完整js
def load_login_js(store, owner, top_js, bottom_js):
    # 賬號
    account = ''
    # 密碼前面文字
    pass_pre = 'login_password.value = '
    # 密碼
    password = ''
    # 第一間店鋪賬號
    if store == '店鋪名1' and owner == '負責人1':
        account = '"賬號";'
        password = '"密碼";'
    '''
    elif store == '店鋪名1' and owner == '負責人2':
        account = '"賬號";'
        password = '"密碼";'
    elif store == '店鋪名1' and owner == '負責人3':
        account = '"賬號";'
        password = '"密碼";'
    # 第二間店鋪賬號
    elif '店鋪名2' and owner == '負責人1':
        account = '"賬號";'
        password = '"密碼";'
    elif '店鋪名2' and owner == '負責人2':
        account = '"賬號";'
        password = '"密碼";'
    elif '店鋪名2' and owner == '負責人3':
        account = '"賬號";'
        password = '"密碼";'
    elif '店鋪名2' and owner == '負責人4':
        account = '"賬號";'
        password = '"密碼";'
    '''
    all_js = top_js + account + pass_pre + password + bottom_js
    # print(all_js)
    return all_js


# 登錄js
login_top_js = load_js('login_top.txt')
login_bottom_js = load_js('login_bottom.txt')
# 未讀詢盤資料串列js
inquiry_js = load_js('inquiry_list.txt')
# TM資料串列js
tm_js = load_js('tm_list.txt')
# 退出登錄js
logout_js = load_js('logout.txt')
# 新詢盤發送內容js
inquiry_reply_send_js = load_js('inq_rep_send.txt')
# 檢查發送新詢盤是否已自動回復js
inquiry_reply_check_js = load_js('inq_rep_che.txt')
# 展開TM聊天框js
tm_open_js = load_js('tm_open.txt')
# 展開新TM對話框js
tm_open_send_js = load_js('tm_open_send.txt')
# 新TM發送內容js
tm_send_reply_js = load_js('tm_send_reply.txt')
# 檢查TM是否自動回復成功js
tm_check_reply_js = load_js('tm_check_reply.txt')


# 將未讀詢盤資料轉化為字串
def inquiry_text(unread_inquiry, browser):
    unread_inquiry_str = '有' + str(len(unread_inquiry)) + '條未讀詢盤:\n\n'  # 最終輸出的未讀詢盤資訊
    for i, val in enumerate(unread_inquiry):
        # print(unread_inquiry[i])
        unread_inquiry_str += '一一一第' + str(i + 1) + '條一一一\n'
        unread_inquiry_str += unread_inquiry[i]['id'] + '\n'
        unread_inquiry_str += unread_inquiry[i]['time'] + '\n'
        unread_inquiry_str += '發起人:' + unread_inquiry[i]['sender'] + '\n'
        unread_inquiry_str += '負責人:' + unread_inquiry[i]['owner'] + '\n'
        unread_inquiry_str += '狀態:' + unread_inquiry[i]['status'] + '\n'
        unread_inquiry_str += '內容:' + unread_inquiry[i]['chat'] + '\n\n'
        cur_time = get_cur_time()
        # 當前小時
        cur_hour = cur_time['cur_hour']
        # 執行自動回復時間
        if cur_hour <= 8 or cur_hour >= 22:
            if unread_inquiry[i]['status'].find("新詢盤") != -1:
                before_check_result = browser_action(unread_inquiry[i]['url'], inquiry_reply_check_js, 'check_send_before', browser)
                if before_check_result == "發送成功":
                    print(before_check_result)
                    unread_inquiry_str = unread_inquiry_str[:-1] + '自動回復:' + before_check_result + '\n\n'
                else:
                    send_result = browser_action('', inquiry_reply_send_js, 'inquiry_reply_send', browser)
                    print(send_result)
                    sleep(7)
                    after_check_result = browser_action('', inquiry_reply_check_js, 'check_send_after', browser)
                    print(after_check_result)
                    unread_inquiry_str = unread_inquiry_str[:-1] + '自動回復:' + after_check_result + '\n\n'
    if len(unread_inquiry) == 0:
        return ''
    else:
        return unread_inquiry_str[:-1].replace(u'\xa0', u' ')


# 將未讀TM資料轉化為字串
def tm_text(unread_tm, browser):
    # 判斷TM聊天框是否已經打開
    tm_open_flag = 0
    unread_tm_str = '有' + str(len(unread_tm)) + '組未讀TM資訊:\n\n'  # 最終輸出的TM資訊
    for i, val in enumerate(unread_tm):
        # print(unread_tm[i])
        unread_tm_str += '一一一第' + str(i + 1) + '組一一一\n'
        unread_tm_str += '咨詢人:' + unread_tm[i]["sender"] + '\n'
        unread_tm_str += '聯系時間:' + unread_tm[i]["contact_time"] + '\n'
        unread_tm_str += '最新資訊:' + unread_tm[i]["latest_msg"] + '\n'
        unread_tm_str += '未讀條數:' + unread_tm[i]["unread_num"] + '\n\n'
        cur_time = get_cur_time()
        # 當前小時
        cur_hour = cur_time['cur_hour']
        # 執行自動回復時間
        if cur_hour <= 8 or cur_hour >= 22:
            if tm_open_flag == 0:
                open_result = browser_action('', tm_open_js, 'tm_open', browser)
                print(open_result)
                sleep(2)
                tm_open_flag = 1
            run_open_send = 'return open_send("' + unread_tm[i]["conversation_id"] + '");'
            global tm_open_send_js
            cur_tm_open_send_js = tm_open_send_js + run_open_send
            open_send_result = browser_action('', cur_tm_open_send_js, 'tm_open_send', browser)
            print(open_send_result)
            send_reply_result = browser_action('', tm_send_reply_js, 'tm_send_reply', browser)
            print(send_reply_result)
            sleep(7)
            check_reply_result = browser_action('', tm_check_reply_js, 'tm_check_reply', browser)
            print(check_reply_result)
            unread_tm_str = unread_tm_str[:-1] + '自動回復:' + check_reply_result + '\n\n'
    if len(unread_tm) == 0:
        return ''
    else:
        return unread_tm_str[:-1].replace(u'\xa0', u' ')


# 瀏覽器執行腳本
def browser_action(target_url, ext_js, run_action, browser):
    # 最大失敗執行次數
    num = 0
    while num < 3:
        try:
            if target_url != '':
                browser.get(target_url)
                sleep(20)
            if run_action == 'tm':
                sleep(30)
            if run_action == "inquiry_reply_send":
                browser.execute_script(ext_js)
                return '新詢盤發送內容成功'
            elif run_action == "tm_send_reply":
                browser.execute_script(ext_js)
                return '新TM發送內容成功'
            return browser.execute_script(ext_js)
        except Exception as e:
            print('運行出錯,原因:', e)
            num += 1
            if num == 3:
                if run_action == 'login':
                    return '登錄失敗'
                elif run_action == 'inquiry':
                    return '獲取未讀詢盤失敗'
                elif run_action == 'tm':
                    return '獲取TM資訊失敗'
                elif run_action == 'logout':
                    return '退出登錄失敗'
                elif run_action == 'check_send_before':
                    return '新詢盤回復前檢測狀態失敗'
                elif run_action == 'inquiry_reply_send':
                    return '新詢盤發送內容失敗'
                elif run_action == 'check_send_after':
                    return '新詢盤回復后檢測狀態失敗'
                elif run_action == 'tm_open':
                    return '展開TM聊天框失敗'
                elif run_action == 'tm_open_send':
                    return '展開新TM對話框失敗'
                elif run_action == 'tm_send_reply':
                    return '新TM發送內容失敗'
                elif run_action == 'tm_check_reply':
                    return '新TM檢測回復狀態失敗'
                else:
                    return '操作失敗'
            if target_url == '':
                sleep(10)
                if run_action == 'tm':
                    browser.get(tm_url)
            else:
                sleep(10)


# 單個賬號獲取資訊邏輯處理
def get_data_actions(store, owner, browser):
    # 轉發資訊
    send_msg = ''
    # 登錄后臺
    login_js = load_login_js(store, owner, login_top_js, login_bottom_js)
    login = browser_action(login_url, login_js, 'login', browser).replace(u'\xa0', u' ')
    login_msg = '【%s】店【%s】' % (store, owner) + login
    print(login_msg)
    if login == '登錄失敗':
        send_msg += '【%s】店【%s】' % (store, owner) + login + '\n'
    sleep(5)
    # 獲取TM資訊
    tm_list = browser_action('', tm_js, 'tm', browser)
    # print(tm_list)
    if type(tm_list) == list:
        tm_list = tm_text(tm_list, browser)
    if tm_list == '':
        tm_msg = ''
        print('【%s】店【%s】沒有未讀TM資訊' % (store, owner))
    else:
        tm_msg = '【%s】店【%s】' % (store, owner) + tm_list
        print(tm_msg)
    if tm_msg != '':
        tm_msg += '\n'
    if store == '店鋪1' and owner == '負責人1(主賬號)' or store == '店鋪2' and owner == '負責人1(主賬號)':
        # 獲取未讀詢盤資訊
        inquiry_list = browser_action(inquiry_url, inquiry_js, 'inquiry', browser)
        # print(inquiry_list)
        if type(inquiry_list) == list:
            inquiry_list = inquiry_text(inquiry_list, browser)
        if inquiry_list == '':
            send_msg += tm_msg
            print('【%s】店沒有未讀詢盤' % store)
        else:
            inquiry_msg = '【%s】店' % store + inquiry_list
            send_msg += inquiry_msg + '\n' + tm_msg
            print(inquiry_msg)
    else:
        send_msg += tm_msg
    '''
    # 退出登錄
    logout = browser_action('', logout_js, 'logout', browser).replace(u'\xa0', u' ')
    logout_msg = '【%s】店【%s】' % (store, owner) + logout
    print(logout_msg)
    if logout == '退出登錄失敗':
        send_msg += '【%s】店【%s】' % (store, owner) + logout + '\n'
    '''
    sleep(5)
    return send_msg


# 單個賬號獲取資訊
def get_one(store, owner, browser):
    result = get_data_actions(store, owner, browser)
    global all_send_msg
    if result.strip() != '':
        all_send_msg += result


# 時間戳轉標準時間
def formal_time(timestamp):
    # 轉換成localtime
    time_local = time.localtime(timestamp)
    # 轉換成新的時間格式
    dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
    return dt


# 獲取現在時間
def get_cur_time():
    now = time.time()
    # print(formal_time(now))
    # 現在時間
    cur_time = formal_time(now)[11:16]
    # print(cur_time)
    cur_hour = int(cur_time[:2])
    cur_min = int(cur_time[3:])
    # print(cur_hour)
    # print(cur_min)
    cur_time_min = cur_hour * 60 + cur_min
    # print(cur_time_min)
    # 下一次執行時間
    if cur_min >= 30:
        next_time_min = (cur_hour + 1) * 60
    else:
        next_time_min = cur_hour * 60 + 30
    # print(next_time_min)
    # 睡眠時間
    sleep_time_min = next_time_min - cur_time_min
    # print(sleep_time_min)
    sleep_time = sleep_time_min * 60
    # print(sleep_time)
    cur_time = {'cur_hour': cur_hour, 'sleep_time': sleep_time}
    return cur_time


logging.basicConfig(level=logging.INFO)
queue_received_message = Queue()


def on_message(message):
    queue_received_message.put(message)


def thread_handle_message(wx_inst):
    while True:
        message = queue_received_message.get()
        print(str(message).encode('gbk', 'ignore').decode('gbk'))


def send_message():
    wx_inst = WechatPCAPI(on_message=on_message, log=logging)
    wx_inst.start_wechat(block=True)

    while not wx_inst.get_myself():
        time.sleep(5)

    print('微信登錄成功')
    print(wx_inst.get_myself())

    threading.Thread(target=thread_handle_message, args=(wx_inst,)).start()

    # 初始化7個瀏覽器
    browser1 = ''
    '''
    browser2 = ''
    browser3 = ''
    browser4 = ''
    browser5 = ''
    browser6 = ''
    browser7 = ''
     '''

    # 打開7個瀏覽器
    for i in range(7):
        # 第n個瀏覽器
        index = i + 1
        # 谷歌瀏覽器配置
        chrome_options = webdriver.ChromeOptions()
        # 自定義用戶目錄
        chrome_options.add_argument('--user-data-dir=C:/ChromeUser/Webdriver/User' + str(index))
        if index == 1:
            browser1 = webdriver.Chrome(executable_path=chromedriver, options=chrome_options)
            browser1.set_page_load_timeout(30)
        '''
        elif index == 2:
            browser2 = webdriver.Chrome(executable_path=chromedriver, options=chrome_options)
            browser2.set_page_load_timeout(30)
        elif index == 3:
            browser3 = webdriver.Chrome(executable_path=chromedriver, options=chrome_options)
            browser3.set_page_load_timeout(30)
        elif index == 4:
            browser4 = webdriver.Chrome(executable_path=chromedriver, options=chrome_options)
            browser4.set_page_load_timeout(30)
        elif index == 5:
            browser5 = webdriver.Chrome(executable_path=chromedriver, options=chrome_options)
            browser5.set_page_load_timeout(30)
        elif index == 6:
            browser6 = webdriver.Chrome(executable_path=chromedriver, options=chrome_options)
            browser6.set_page_load_timeout(30)
        elif index == 7:
            browser7 = webdriver.Chrome(executable_path=chromedriver, options=chrome_options)
            browser7.set_page_load_timeout(30)
         '''
    while 1:
        cur_time = get_cur_time()
        # 睡眠時間
        sleep_time = cur_time['sleep_time']
        # 清空轉發資訊
        global all_send_msg
        all_send_msg = ''
        # 執行緒串列
        threads = []
        for i in range(7):
            # 第n個賬號
            index = i + 1
            # 獲取chinatouch店資訊
            if index == 1:
                thread = threading.Thread(target=get_one, args=('店鋪名1', '負責人1', browser1,))
                threads.append(thread)
            '''
            elif index == 2:
                thread = threading.Thread(target=get_one, args=('店鋪名1', '負責人2', browser2,))
                threads.append(thread)
            elif index == 3:
                thread = threading.Thread(target=get_one, args=('店鋪名1', '負責人3', browser3,))
                threads.append(thread)
            # 獲取leangle店資訊
            elif index == 4:
                thread = threading.Thread(target=get_one, args=('店鋪名2', '負責人1', browser4,))
                threads.append(thread)
            elif index == 5:
                thread = threading.Thread(target=get_one, args=('店鋪名2', '負責人2', browser5,))
                threads.append(thread)
            elif index == 6:
                thread = threading.Thread(target=get_one, args=('店鋪名2', '負責人3', browser6,))
                threads.append(thread)
            elif index == 7:
                thread = threading.Thread(target=get_one, args=('店鋪名2', '負責人4', browser7,))
                threads.append(thread)
            '''
        # 執行緒開始
        for thread in threads:
            thread.start()
        # 執行緒阻塞
        for thread in threads:
            thread.join()
        # 去除末尾換行符
        all_send_msg = all_send_msg.strip()
        print('---全部轉發資訊---')
        if all_send_msg == '':
            print('暫時沒有未讀詢盤和TM資訊')
        else:
            print(all_send_msg)
        print('----------------')
        if all_send_msg != '':
            all_send_msg = all_send_msg.encode('gbk', 'ignore').decode('gbk')
            wx_inst.send_text(to_user='要轉發到的群', msg=all_send_msg)
        # 下一次執行時間
        sleep(sleep_time)


if __name__ == '__main__':
    # 全部轉發資訊
    all_send_msg = ''
    send_message()

上面腳本中的賬號、密碼、店鋪名、負責人、要轉發到的群需要在對應的位置進行改動填入,另外,不能洗掉專案檔案夾下的OpenWechatMulti.exe、WechatPCHelper.dll、WechatPCAPI.pyd,程式呼叫微信需要用到,如果運行不成功,請關閉防火墻后再試,

阿里國際站后臺監控專案檔案下載地址: 阿里國際站后臺監控

提取碼: vmmg

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/163163.html

標籤:其他

上一篇:使用python把批量xls檔案轉換為xlsx

下一篇:python爬取代理IP并進行有效的IP測驗

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 面試突擊第一季,第二季,第三季

    第一季必考 https://www.bilibili.com/video/BV1FE411y79Y?from=search&seid=15921726601957489746 第二季分布式 https://www.bilibili.com/video/BV13f4y127ee/?spm_id_fro ......

    uj5u.com 2020-09-10 05:35:24 more
  • 第三單元作業總結

    1.前言 這應該是本學期最后一次寫作業總結了吧。總體來說,對作業的節奏也差不多掌握了,作業做起來的效率也更高了。雖然和之前的作業一樣,作業中都要用到新的知識,但是相比之前,更加懂得了如何利用工具以及資料。雖然之間卡過殼,但總體而言,這幾次作業還算完成的比較好。 2.作業程序總結 相比前兩個單元,此單 ......

    uj5u.com 2020-09-10 05:35:41 more
  • 北航OO(2020)第四單元博客作業暨課程總結博客

    北航OO(2020)第四單元博客作業暨課程總結博客 本單元作業的架構設計 在本單元中,由于UML圖具有比較清晰的樹形結構,因此我對其中需要進行查詢操作的元素進行了包裝,在樹的父節點中存盤所有孩子的參考。考慮到性能問題,我采用了快取機制,一次查詢后盡可能快取已經遍歷過的資訊,以減少遍歷次數。 本單元我 ......

    uj5u.com 2020-09-10 05:35:48 more
  • BUAA_OO_第四單元

    一、UML決議器設計 ? 先看下題目:第四單元實作一個基于JDK 8帶有效性檢查的UML(Unified Modeling Language)類圖,順序圖,狀態圖分析器 MyUmlInteraction,實際上我們要建立一個有向圖模型,UML中的物件(元素)可能與同級元素連接,也可與低級元素相連形成 ......

    uj5u.com 2020-09-10 05:35:54 more
  • 6.1邏輯運算子

    邏輯運算子 1. && 短路與 運算式1 && 運算式2 01.運算式1為true并且運算式2也為true 整體回傳為true 02.運算式1為false,將不會執行運算式2 整體回傳為false 03.只要有一個運算式為false 整體回傳為false 2. || 短路或 運算式1 || 運算式2 ......

    uj5u.com 2020-09-10 05:35:56 more
  • BUAAOO 第四單元 & 課程總結

    1. 第四單元:StarUml檔案決議 本單元采用了圖模型決議UML。 UML檔案可以抽象為圖、子圖、邊的邏輯結構。 在實作中,圖的節點包括類、介面、屬性,子圖包括狀態圖、順序圖等。 采用了三次遍歷UML元素的方法建圖,第一遍遍歷建點,第二、三次遍歷設定屬性、連邊,實作圖物件的初始化。這里借鑒了一些 ......

    uj5u.com 2020-09-10 05:36:06 more
  • 談談我對C# 多型的理解

    面向物件三要素:封裝、繼承、多型。 封裝和繼承,這兩個比較好理解,但要理解多型的話,可就稍微有點難度了。今天,我們就來講講多型的理解。 我們應該經常會看到面試題目:請談談對多型的理解。 其實呢,多型非常簡單,就一句話:呼叫同一種方法產生了不同的結果。 具體實作方式有三種。 一、多載 多載很簡單。 p ......

    uj5u.com 2020-09-10 05:36:09 more
  • Python 資料驅動工具:DDT

    背景 python 的unittest 沒有自帶資料驅動功能。 所以如果使用unittest,同時又想使用資料驅動,那么就可以使用DDT來完成。 DDT是 “Data-Driven Tests”的縮寫。 資料:http://ddt.readthedocs.io/en/latest/ 使用方法 dd. ......

    uj5u.com 2020-09-10 05:36:13 more
  • Python里面的xlrd模塊詳解

    那我就一下面積個問題對xlrd模塊進行學習一下: 1.什么是xlrd模塊? 2.為什么使用xlrd模塊? 3.怎樣使用xlrd模塊? 1.什么是xlrd模塊? ?python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。 今天就先來說一下xl ......

    uj5u.com 2020-09-10 05:36:28 more
  • 當我們創建HashMap時,底層到底做了什么?

    jdk1.7中的底層實作程序(底層基于陣列+鏈表) 在我們new HashMap()時,底層創建了默認長度為16的一維陣列Entry[ ] table。當我們呼叫map.put(key1,value1)方法向HashMap里添加資料的時候: 首先,呼叫key1所在類的hashCode()計算key1 ......

    uj5u.com 2020-09-10 05:36:38 more
最新发布
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:20:47 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:20:25 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:20:17 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:20:10 more
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:19:44 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:19:07 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:18:57 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:18:49 more
  • 05單件模式

    #經典的單件模式 public class Singleton { private static Singleton uniqueInstance; //一個靜態變數持有Singleton類的唯一實體。 // 其他有用的實體變數寫在這里 //構造器宣告為私有,只有Singleton可以實體化這個類! ......

    uj5u.com 2023-04-19 08:42:51 more
  • 【架構與設計】常見微服務分層架構的區別和落地實踐

    軟體工程的方方面面都遵循一個最基本的道理:沒有銀彈,架構分層模型更是如此,每一種都有各自優缺點,所以請根據不同的業務場景,并遵循簡單、可演進這兩個重要的架構原則選擇合適的架構分層模型即可。 ......

    uj5u.com 2023-04-19 08:42:41 more