戴口罩

初衷

为了提高各大群众的口罩购买成功概率,特此制作该教程!

中国口罩产能虽然世界第一,占比高达50%,但此次冠状病毒疫情来势迅猛,使得口罩需求爆发式增长,再恰好赶上春节工厂放假停工,肯定供不应求。 因此口罩的问题迎面而来,而创建这个教程的初衷也是为了给那些没有口罩的人一个买口罩的机会,提高购买口罩的成功率!


教程

1、首先准备python环境

Python下载后安装教程(windows为例):

在Windows环境下安装Python

1.下载完成后打开安装包并执行,运行出该界面。

2.我们这里选择自定义安装,并全选安装,其中安装pip,

可使用“Tornado”web框架等模块

3.单击“完成”

Windows安装比较简单


2、准备chorm浏览器和selenium环境

1、安装selenium

执行 python3 -m pip install selenium

2、安装chrome驱动

驱动下载地址:http://docs.seleniumhq.org/download/选择对应系统的浏览器驱动

或者直接到
https://sites.google.com/a/chromium.org/chromedriver/下载最新版mac chrome驱动(需翻墙);

ChromeDriver
webdriver的是跨越许多浏览器Web应用程序的自动化测试的开源工具。它提供的功能,用于导航到网页上,用户输入,JavaScript执行,等等。ChromeDriver是实现一个独立的服务器的webdriver的线协议铬。 我们在实施和移动的过程W3C标准。ChromeDriver适用于Android版Chrome和Chrome在桌面上(苹果机,Linux,Windows和Chrome)。 您可以查看webdriver的标准实施现况这里。 最新版本:ChromeDriver 2.33 在所有可用的版本下载 ChromeDriver文档 入门ChromeDriver在桌面上(在Windows,Mac和Linux) ChromeDriver与Android ChromeDriver与ChromeOS的 ChromeOptions,ChromeDriver的能力 移动仿真 Chrome扩展程序安装 详细的日志记录和性能数据记录

3、配置chrome驱动

将解压后的chromedriver移动到/usr/local/bin目录下
否则在运行脚本时,会提示
chromedriver executable needs to be in PATH错误

3、编写代码,功能模块如下

  • 登录验证模块
def login():
    # 打开淘宝首页,通过扫码登录
    browser.get("https://www.taobao.com")
    time.sleep(3)
    if browser.find_element_by_link_text("亲,请登录"):
        browser.find_element_by_link_text("亲,请登录").click()
        print(f"请尽快扫码登录")
        time.sleep(10)
  • 自动购买下单模块
def buy():
num=0
while num<15:
try:
print(f'类型寻找中' + str(num))
type = browser.find_element_by_xpath(
'//*[@id="J_DetailMeta"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li[' + str(num) + ']').text
print(f'选择类型')
browser.find_element_by_xpath(
'//*[@id="J_DetailMeta"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li[' + str(num) + ']').click()
choice(num)
stock = browser.find_element_by_id("J_EmStock").text
print(stock)
print(type)
browser.find_element_by_id("J_LinkBuy").click()
time.sleep(1)
browser.find_element_by_class_name("go-btn").click()
winsound.Beep(500, 2000)
return 1
break
except:
num=num+1
print(num)
  • 库存实时监测模块
def mon(url):
browser.get(url)
global state
js1 = '''Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) '''
js2 = '''window.navigator.chrome = { runtime: {},  }; '''
js3 = '''Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); '''
js4 = '''Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], }); '''
browser.execute_script(js1)
browser.execute_script(js2)
browser.execute_script(js3)
browser.execute_script(js4)
time.sleep(3)
try:
stock = browser.find_element_by_id("J_EmStock").text
print(stock)
state=1
except:
print(f'没有找到库存信息')
state=0
  • 类型选择模块
def choice(num):
source=browser.page_source
if "已选择" in source:
print('已选择')
return 1
else:
print('没有发现选中目标')
browser.find_element_by_xpath(
'//*[@id="J_DetailMeta"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li[' + str(num) + ']').click()

4、调试并运行,可以看见自动登录浏览器,并且弹出扫码界面让我们登录淘宝

https://login.m.taobao.com/qrcodeCheck.htm?lgToken=1c9395ab8eb5be496a3220dbaf3c24b0&tbScanOpenType=Notification (二维码自动识别)

5、登陆后,会循环监控库存信息

6、当有库存时候就会自动抢购成功,我们只需要支付货款即可!

源代码(可运行):

from selenium import webdriver #line:1
import time #line:2
import winsound #line:3
option =webdriver .ChromeOptions ()#line:5
option .add_experimental_option ('excludeSwitches',['enable-automation'])#line:6
browser =webdriver .Chrome (chrome_options =option )#line:7
WIDTH =320 #line:8
HEIGHT =640 #line:9
PIXEL_RATIO =3.0 #line:10
UA ='Mozilla/5.0 (Linux; Android 4.1.1; GT-N7100 Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/35.0.1916.138 Mobile Safari/537.36 T7/6.3'#line:11
mobileEmulation ={"deviceMetrics":{"width":WIDTH ,"height":HEIGHT ,"pixelRatio":PIXEL_RATIO },"userAgent":UA }#line:14
options =webdriver .ChromeOptions ()#line:15
options .add_experimental_option ('mobileEmulation',mobileEmulation )#line:16
drive =webdriver .Chrome (chrome_options =options )#line:17
times =0 #line:18
def monphone (O0O00OOOOOO000O0O ):#line:19
    global times #line:20
    drive .get (O0O00OOOOOO000O0O )#line:21
    O0000O000OOO00OO0 =drive .find_element_by_xpath ('//*[@id="s-actionBar-container"]/div/div[2]/a[3]').text #line:22
    print (O0000O000OOO00OO0 )#line:23
    times =times +1 #line:24
    return O0000O000OOO00OO0 #line:25
def login ():#line:27
    browser .get ("https://www.taobao.com")#line:29
    time .sleep (3 )#line:30
    if browser .find_element_by_link_text ("亲,请登录"):#line:31
        browser .find_element_by_link_text ("亲,请登录").click ()#line:32
        print (f"请尽快扫码登录")#line:33
        time .sleep (10 )#line:34
def mon (OO0OO00OOOOO0000O ):#line:37
    browser .get (OO0OO00OOOOO0000O )#line:38
    global state #line:39
    OO0OOO0O00O000OOO ='''Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) '''#line:40
    OO00OOO0O0OOOOO00 ='''window.navigator.chrome = { runtime: {},  }; '''#line:41
    OO0OO000O00OOO0OO ='''Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); '''#line:42
    O0O0O0OOO000O0OO0 ='''Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], }); '''#line:43
    browser .execute_script (OO0OOO0O00O000OOO )#line:44
    browser .execute_script (OO00OOO0O0OOOOO00 )#line:45
    browser .execute_script (OO0OO000O00OOO0OO )#line:46
    browser .execute_script (O0O0O0OOO000O0OO0 )#line:47
    time .sleep (3 )#line:48
    try :#line:49
        OO00OOOOO0O00OOO0 =browser .find_element_by_id ("J_EmStock").text #line:50
        print (OO00OOOOO0O00OOO0 )#line:51
        state =1 #line:52
    except :#line:53
        print (f'没有找到库存信息')#line:54
        state =0 #line:55
def clear ():#line:57
        drive .delete_all_cookies ()#line:58
        global times #line:59
        times =0 #line:60
        print ("已清除缓冲区")#line:61
def choice (O0O000OO0OOO0OOOO ):#line:63
    OOO0O000OOOO00OO0 =browser .page_source #line:64
    if "已选择"in OOO0O000OOOO00OO0 :#line:65
        print ('已选择')#line:66
        return 1 #line:67
    else :#line:68
        print ('没有发现选中目标')#line:69
        browser .find_element_by_xpath ('//*[@id="J_DetailMeta"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li['+str (O0O000OO0OOO0OOOO )+']').click ()#line:71
def buy ():#line:73
    O00000O00O0O00000 =0 #line:74
    while O00000O00O0O00000 <15 :#line:75
        try :#line:76
            print (f'类型寻找中'+str (O00000O00O0O00000 ))#line:77
            OO0OO0OO00OOO00O0 =browser .find_element_by_xpath ('//*[@id="J_DetailMeta"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li['+str (O00000O00O0O00000 )+']').text #line:79
            print (f'选择类型')#line:80
            browser .find_element_by_xpath ('//*[@id="J_DetailMeta"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li['+str (O00000O00O0O00000 )+']').click ()#line:82
            choice (O00000O00O0O00000 )#line:83
            OO00OO00000OOOO0O =browser .find_element_by_id ("J_EmStock").text #line:84
            print (OO00OO00000OOOO0O )#line:85
            print (OO0OO0OO00OOO00O0 )#line:86
            browser .find_element_by_id ("J_LinkBuy").click ()#line:87
            time .sleep (1 )#line:88
            browser .find_element_by_class_name ("go-btn").click ()#line:89
            winsound .Beep (500 ,2000 )#line:90
            return 1 #line:91
            break #line:92
        except :#line:93
            O00000O00O0O00000 =O00000O00O0O00000 +1 #line:94
            print (O00000O00O0O00000 )#line:95
login ()#line:100
url =input ()#line:101
while True :#line:102
    state =monphone (url )#line:103
    if times >=100 :#line:104
        clear ()#line:105
    if state =="立即购买":#line:106
        mon (url )#line:107
        if state ==1 :#line:108
            success =buy ()#line:109
            if success ==1 :#line:110
                print ('成功')#line:111
                winsound .Beep (500 ,2000 )#line:112
                break #line:113

如果还是不太会或者嫌太麻烦,可以只使用这些监控库存的软件。

口罩库存监控-抢口罩软件教程



下载:

PS:由于服务器压力过大,现决定付费过滤弱需求人群!请有需要的自行选择方案下载使用。

购买



结语

希望能早日买到自己的口罩,疫情早日散去!

Popmars