阿里國際站地址:阿里國際站
專案背景:
用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":"")[1].split(""")[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":"")[1].split(""")[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/qita/163563.html
標籤:其他
下一篇:python爬蟲請求頭
