自动抢购脚本制作

前些日子接了个抢购脚本制作的活,抢购的基本流程是每天7点登录后点击抢购然后会出来一排入口,然后点击其中的入口就可以完成抢购操作。

使用selenium,python2 制作脚本,打包exe运行,在到点前运行exe程序,然后用户需要手动完成登录,然后到点之后会自动刷新抢单。

需要参考的可以随便用,不过最后打包后得给用户附送一个chromedriver,并要求用户安装对应版本的chrome。

代码如下

# -*- coding: utf-8 -*-
"""
File Name:     taobao_craw
Description :
Author :       meng_zhihao
date:          2018/9/4
淘宝店地址 :https://shop560916306.taobao.com/?spm=2013.1.1000126.2.3418ab83wv5Ai2
"""
import requests
from lxml import etree
import json
import urllib
import time
import random
import datetime
import csv
import os
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.alert import Alert
import sys
reload(sys)
sys.setdefaultencoding('utf8')

import ConfigParser
import re
import datetime


class ChromeOperate():
    def __init__(self,url='',executable_path='',User_data_dir='',arguments=[],headless=True):
        option = webdriver.ChromeOptions()
        if User_data_dir:
            option.add_argument( '--user-data-dir=%s'%User_data_dir)  # 设置成用户自己的数据目录
        else:
            import getpass
            username = getpass.getuser()
            default_path = 'C:\Users\%s\AppData\Local\Google\Chrome\User Data'%username  #echo %LOCALAPPDATA%\Google\Chrome\User Data
            if os.path.exists(default_path):
                #option.add_argument('--user-data-dir=%s' % default_path)
                pass
        option.add_argument('--start-maximized')
        if headless:option.add_argument('headless')
        option.add_argument('google-base-url=%s' % 'https://www.baidu.com/')
        for argument in arguments:
            option.add_argument(argument)
        if not executable_path:executable_path=r'C:\Users\Administrator\Desktop\chromedriver.exe'
        self.driver = webdriver.Chrome(executable_path=executable_path,chrome_options=option)

        if url:self.open(url)


    def open(self,url):
        self.driver.get(url) #   self.driver.get(url).page_source

    def open_source(self):
        return self.driver.page_source

    def title(self):
        self.title=self.driver.title
        return self.title

    def quit(self):
        self.driver.quit()

    def find_element_by_name(self,name):
        return self.driver.find_element_by_name(name)

    def find_elements_by_xpath(self,xpath): #貌似不能用/text
        return self.driver.find_elements_by_xpath(xpath)

    def find_element_by_id(self,id):
        try:
            return self.driver.find_element_by_id(id)
        except:
            return None

    def input_words(self,element,words):
        element.clear()
        element.send_keys(words)

    def click_by_id(self,id):
        self.driver.find_element_by_id(id).click()

    def send_file(self,element,path):
        element.sendKeys(path);

    def wait_element(self,element_id):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.ID, element_id))
        )

    def get_title(self):
        print(self.driver.title)
        return self.driver.title

    def refresh(self):
        self.driver.refresh()  #

USER_AGENT_POOL=[
    'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
    "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
    "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
    "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
    "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
    "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
    "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
    "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)",
    "Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 6.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER",
    "Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
    "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
    "Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)",
    "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
    "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
    "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
    "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
    "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre",
    "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
    "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",

    ]
cop = ChromeOperate(executable_path=r'chromedriver.exe',headless=False)





if __name__=='__main__':
    timestamp=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    print timestamp
    #max_num = int(mainconf.get('conf', 'max_num'))
    cop.open('http://www.xinqiyang.cn/Home/Login/index')
    while True:
        time.sleep(1)
        if '欢迎使用共享富' in cop.open_source(): #验证码不要输错
            print '登录完成'
            break
    cop.open('http://www.xinqiyang.cn/Home/Index/jys')
    while True:
        time.sleep(1)
        if (datetime.datetime.now().hour>=18 and datetime.datetime.now().minute>=59) or (datetime.datetime.now().hour>=19):
            break
    while True:
        cop.open('http://www.xinqiyang.cn/Home/Myuser/qdc')
        try:
            source = cop.open_source()
            print 'try'
            break
        except:
            a = Alert(cop.driver)
            a.accept()

    for i in range(5,25,1):
        cop.open('http://www.xinqiyang.cn/Home/Myuser/grab/name/%s'%str(i))
        print 'http://www.xinqiyang.cn/Home/Myuser/grab/name/%s'%str(i)
        try:
            source = cop.open_source()
            break
        except:
            a = Alert(cop.driver)
            a.accept()

    for i in range(1,10,1):
        cop.open('http://www.xinqiyang.cn/Home/Myuser/grab/name/%s'%str(i))
        print 'http://www.xinqiyang.cn/Home/Myuser/grab/name/%s' % str(i)
        try:
            source = cop.open_source()
            break
        except:
            a = Alert(cop.driver)
            a.accept()


展开阅读全文

没有更多推荐了,返回首页