import csv import time import requests from selenium import webdriver import random from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.wait import WebDriverWait import base64 import hmac from urllib.parse import quote_plus import hashlib import json import re import os from jiance_varg import * ''' 1.读取表格数据中的isbn,书名,出版社,作者,出版时间,原价 2.通过书名去查询数据,获取列表中的书名,出版社,作者,出版时间,原价来获取,有货的点进去下单,同时发送消息 3.重复上述步骤 ''' # send_data = '125.94.215.14' # dingding_url = 'https://oapi.dingtalk.com/robot/send?access_token=4239c3263754ef3c6651d3794111caffcd9fe665faa7e2c3cac9583451dee306' # dingding_secret = 'SECf8ffe2ebc9f8ab6f587b8da757169eeb3fee3102b42efe619edd0c6110132264' # # img_port = 'http://125.94.215.14:8001/' xiadan_book = '' csv_path = "多抓鱼数据存档.csv" #是否加入购物车的全局标记 cart_index = 0 erweima = "" def send_content(result): try: url = quchong_url data = {"name":result} headers = {"Content-Type":"application/json"} rsp = requests.post(url,json = data,headers = headers) return rsp.status_code except: return 500 def read_csv_standard(file_path): data = [] with open(file_path, 'r',encoding="gbk") as file: reader = csv.reader(file) for row in reader: data.append(row) return data # data = read_csv_standard("多抓鱼数据存档.csv") # print(data) ''' 1.启动浏览器,多抓鱼搜索界面 2.搜索数据,获取数据的所有信息,一致的进行处理,找到的话,就点进去确定,找不到的话,就放弃(放弃的数据要不要直接从表里面剔除,再考虑) 3.详情界面,确定是否有货且数据核对是否一致。有货的点击加入购物车。要么弹出选择,要么添加成功。 4.去购物车界面锁定书籍,同时发送钉钉,然后回到搜索界面继续搜索 ''' #启动浏览器,进入多抓鱼搜索界面 def open_chrome(): #无头模式 #获取浏览器cookie user_data_dir = r'--user-data-dir=C:\Users\S2020\AppData\Local\Google\Chrome\User Data2' # # # 加载配置数据 option = webdriver.ChromeOptions() option.add_argument(user_data_dir) browser = webdriver.Chrome(chrome_options=option) # => 注意这里的参数 #browser = webdriver.Chrome() # => 注意这里的参数 browser.get('https://www.duozhuayu.com/search/book') browser.maximize_window() time.sleep(10) return browser def retain_alphanumeric_chinese(input_str): return re.sub(r'[^a-zA-Z0-9\u4e00-\u9fa5]', '', input_str) #获取书籍详情页的信息并进行对比,有货的,加入购物车,没货的,核对是否是确定数据 #只有数据不一致的返回false,其他情况返回true def check_data(browser,scv_data): global cart_index time.sleep(3) try: name_book = browser.find_element_by_tag_name("h1").text msg = browser.find_elements_by_class_name("info-row") sale_price, chubanshe, date, zuozhe = '0', '0', '0', '0' for i in msg: if "原价" in i.text: sale_price = i.text.replace("原价\n¥", "") if "出版社" in i.text: chubanshe = retain_alphanumeric_chinese(i.text.replace("出版社\n", "")) if "出版\n" in i.text: date = i.text.replace("出版\n", "").replace("-","") if "作者\n" in i.text: zuozhe = retain_alphanumeric_chinese(i.text.replace("作者\n", "")) print(sale_price, chubanshe, date, zuozhe,name_book) print(scv_data) if scv_data["sale_price"] == sale_price: print("sale_price is success") if scv_data["chubanshe"] == chubanshe: print("chubanshe is success") if scv_data["date"] == date: print("date is success") if scv_data["zuozhe"] == zuozhe: print("zuozhe is success") if scv_data["name"] == name_book: print("name is success") if scv_data["sale_price"] == sale_price and scv_data["chubanshe"] == chubanshe and scv_data["date"] == date and scv_data["zuozhe"] == zuozhe and scv_data["name"] == name_book: print("校验成功") book_flag = browser.find_elements_by_class_name("Button-inner")[-1] if book_flag.text == '加入购物车': pinxiang = browser.find_element_by_class_name("book-quality").text if "良好" == pinxiang or "轻度污渍" == pinxiang or "轻度磨损或破损" == pinxiang or "轻度污渍、轻度磨损或破损" == pinxiang: book_flag.click() cart_index = 1 elif book_flag.text == '多个品相可选': book_flag.click() time.sleep(0.5) pinxiang = browser.find_element_by_class_name("goods-row").find_element_by_class_name("condition").text pinxiang = pinxiang[pinxiang.index(":")+1:] print(pinxiang) if "品相良好" == pinxiang or "轻度污渍" == pinxiang or "轻度磨损或破损" == pinxiang or "轻度污渍、轻度磨损或破损" == pinxiang: browser.find_elements_by_class_name("Button-inner")[-1].click() cart_index = 1 browser.back() return True else: print("校验失败") browser.back() return False except: print("校验失败") browser.back() return False def search_book_data(browser,scv_data): #搜索 try: browser.find_element_by_class_name("search-input").click() time.sleep(random.randint(1,3)) browser.find_element_by_class_name("search-input").clear() time.sleep(0.5) browser.find_element_by_class_name("search-input").send_keys(scv_data["name"]) time.sleep(random.randint(1,3)) browser.find_element_by_class_name("search-input").send_keys(Keys.ENTER) time.sleep(random.randint(2,3)) except: print("22222") return 0 #获取数据列表 try: search_result = browser.find_element_by_class_name("search-results") except: print("没获取到数据列表") return 0 #数据列表判断:没有有货的,直接放弃,有有货的,一个,多个,分别进行处理 book_true = search_result.find_element_by_class_name("instock-list") book_true_num = book_true.find_elements_by_class_name("SearchBookItem") print(len(book_true_num)) if len(book_true_num) == 1: book_true_num[0].find_element_by_class_name("SearchBookItem-title").click() check_data(browser, scv_data) elif len(book_true_num) == 0: return 0 #获取列表,以及列表中的数据,进行对比 zhekuai 还没调试 elif len(book_true_num) > 1: index_list = [] titles = book_true.find_elements_by_class_name("SearchBookItem-title") mescs = book_true.find_elements_by_class_name("SearchBookItem-description") for index,title in enumerate(titles): book_text = retain_alphanumeric_chinese(mescs[index].text) if scv_data["name"] != title.text: continue if scv_data["zuozhe"] in book_text or scv_data["chubanshe"] in book_text: index_list.append(index) # check_data(browser,scv_data) print(index_list) for index in index_list: print(index) try: browser.find_element_by_class_name("search-results").find_element_by_class_name("instock-list").find_elements_by_class_name("SearchBookItem")[index].find_element_by_class_name("SearchBookItem-title").click() except: break re = check_data(browser, scv_data) time.sleep(2) if re: break #加入到购物车的书籍下单锁定 def new_order(browser,sell_book): ''' 对应数据下单 :param browser: :return: ''' browser.get("https://www.duozhuayu.com/cart") wait = WebDriverWait(browser, 5, 0.5) wait.until(lambda browser:browser.find_elements_by_class_name("SelItem")) sell_books = browser.find_elements_by_class_name("SelItem") print(len(sell_books)) if len(sell_books) > 1: for i in range(0,len(sell_books)): try: sell_books[i].find_element_by_class_name("book-title") print("sell_books") except: print("failed get book") continue print(sell_books[i].find_element_by_class_name("book-title").text) print(sell_book) if sell_books[i].find_element_by_class_name("book-title").text == sell_book: print("获取一致数据") action = sell_books[i].find_element_by_class_name("action") browser.execute_script('arguments[0].scrollIntoView(true)', action) action.click() # 设置等待 wait = WebDriverWait(browser, 5, 0.5) # 使用匿名函数 x = sell_books[i] wait.until(lambda x: x.find_element_by_class_name("Button--clay")) time.sleep(0.5) browser.find_elements_by_class_name("Button-inner")[-1].click() wait.until(lambda x: x.find_element_by_class_name("Button--clay")) time.sleep(0.5) browser.find_elements_by_class_name("Button-inner")[-1].click() time.sleep(2) if not browser.find_elements_by_xpath("//*[contains(text(),'支付编号')]"): print("没获取到支付二维码,没抢到书") return False global erweima erweima = str(int(time.time())) + "erweima.jpg" browser.save_screenshot(erweima) browser.refresh() wait.until(lambda browser: browser.find_elements_by_class_name("SelItem")) time.sleep(3) break def send_dingding(server_msg): ''' 发送短信给主人 :return: ''' # 定义请求的URL timestamp = str(round(time.time() * 1000)) url = dingding_url secret = dingding_secret secret_enc = secret.encode('utf-8') string_to_sign = '{}\n{}'.format(timestamp, secret) string_to_sign_enc = string_to_sign.encode('utf-8') hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = quote_plus(base64.b64encode(hmac_code)) headers = {'Content-Type': 'application/json'} webhook = url + '×tamp=' + timestamp + '&sign=' + sign msg_data = '以下书籍已下单,请尽快核实付款:\n' msg_data = msg_data + server_msg + "\n" text = msg_data + "\n蛋蛋新通知:您ip为" + send_data + '的服务器所挂载的账号查找到了新货,请尽快下单' data = { "msgtype": "text", "text": { "content": "%s" % (text) } } value = json.dumps(data) r = requests.post(webhook, value, headers=headers) def send_dingding_img(img_url): timestamp = str(round(time.time() * 1000)) url = dingding_url secret = dingding_secret secret_enc = secret.encode('utf-8') string_to_sign = '{}\n{}'.format(timestamp, secret) string_to_sign_enc = string_to_sign.encode('utf-8') hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = quote_plus(base64.b64encode(hmac_code)) headers = {'Content-Type': 'application/json'} webhook = url + '×tamp=' + timestamp + '&sign=' + sign # 构造消息体 message = { "msgtype": "markdown", "markdown": { "title": "test", "text": "![screenshot](%s)"%(img_url) }, } response = requests.post(webhook, json=message,headers = headers) def retain_alphanumeric_chinese(input_str): return re.sub(r'[^a-zA-Z0-9\u4e00-\u9fa5]', '', input_str) def delete_files(directory, extension): for root, dirs, files in os.walk(directory): for file in files: if file.endswith('.' + extension): path = os.path.join(root, file) os.remove(path) print("已删除文件:", path) if __name__ == "__main__": current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print(current_time) delete_files('C://多抓鱼', 'png') delete_files('C://多抓鱼', 'jpg') csv_datas = read_csv_standard(csv_path) print(csv_datas) # for i in csv_datas: # for j in i: # print(j) # print(retain_alphanumeric_chinese(j)) # time.sleep(200) data_dict = { "sale_price": "", "name": "", "zuozhe": "", "chubanshe": "", "date": "", } browser = open_chrome() time.sleep(10) s = 0 number_index = 1 while 1: if number_index > 1500: break for csv_data in csv_datas: if number_index > 1500: break current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print(current_time) print("已处理数量:"+str(number_index)) number_index = number_index + 1 time.sleep(random.randint(3, 5)) s = s + 1 if s >=100: s = 0 browser.quit() time.sleep(random.randint(60, 120)) browser = open_chrome() data_dict["sale_price"] = csv_data[5] if "." not in data_dict["sale_price"]: data_dict["sale_price"] = data_dict["sale_price"] + ".00" data_dict["chubanshe"] = retain_alphanumeric_chinese(csv_data[6]) data_dict["date"] = csv_data[7] data_dict["zuozhe"] = retain_alphanumeric_chinese(csv_data[8]) data_dict["name"] = csv_data[9] search_book_data(browser,data_dict) if cart_index == 1: print("cart_index:" + str(cart_index)) try: new_order(browser,data_dict["name"]) except: print("获取列表失败") status = send_content(data_dict["name"]) if status == 200: send_dingding(data_dict["name"]) img_url = img_port + erweima send_dingding_img(img_url) xiadan_book = str(int(time.time())) + "book_xiadan.png" browser.save_screenshot(xiadan_book) img_url = img_port + xiadan_book send_dingding_img(img_url) cart_index = 0 browser.get('https://www.duozhuayu.com/search/book') # break