There are quite a lot of concerts recently, today I will share how to use Python to automatically grab tickets!

picture

There are quite a lot of concerts recently, all of which are well-known singers that everyone likes, so I specially made a tutorial to help everyone (mainly because my cousin wants to chase after the goddess, so I taught him to grab tickets by himself)

picture

knowledge points

  • Selenium Taobao slider processing
  • Realization of buying logic

Necessary environment

  • python 3.8
  • pycharm professional edition
  • Google Chrome + Google Drive + selenium3.141.0
  • stealth.min.js

code display

I also recorded a video to explain in detail, packaged it together with the source code, and sent me a private message to receive it automatically.

module

import time
from info import PHONE, PASSWORD
from selenium import webdriver

Get the moving trajectory, imitate the sliding behavior of people, first accelerate and then decelerate uniformly.

def get_tracks(distance):
    # 初速度
    v = 5
    # 单位时间为0.2s来统计轨迹,轨迹即0.2内的位移
    t = 0.2
    # 位移/轨迹列表,列表内的一个元素代表0.2s的位移
    tracks = []
    # 当前的位移
    current = 0
    # 到达mid值开始减速
    mid = distance * 4 / 5
    while current < distance:
        if current < mid:
            # 加速度越小,单位时间的位移越小,模拟的轨迹就越多越详细
            a = 8
        else:
            a = -3
        # 初速度
        v0 = v
        # 0.2秒时间内的位移
        s = v0 * t + 0.5 * a * (t ** 2)
        # 当前的位置
        current += s
        # 添加到轨迹列表
        tracks.append(round(s))
        # 速度已经达到v,该速度作为下次的初速度
        v = v0 + a * t
    return tracks

log in

# 1.1 打开浏览器
driver = webdriver.Chrome(options=options)
f = open('stealth.min.js', mode='r', encoding='utf-8').read()
# 移除selenium当中爬虫的特征
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': f})
# 1.2 打开登陆网页
driver.get(login_url)
# 1.3 输入账号, 输入密码, 点击登陆 (滑块)
try:
    # 进入内嵌页面
    driver.switch_to.frame(0)
    # 账号输入框: #fm-login-id
    driver.find_element_by_css_selector('#fm-login-id').send_keys(PHONE)
    # 密码输入框: #fm-login-password
    driver.find_element_by_css_selector('#fm-login-password').send_keys(PASSWORD)
    time.sleep(1)
    # 为什么会出现滑块, 用selenium打开的浏览器, 和正常用户打开的浏览器不同
    # 过滑块
    driver.switch_to.frame(0)
    slider = driver.find_element_by_css_selector('#nc_1_n1z')
    # 让鼠标执行 点击并且保持按住元素slider
    webdriver.ActionChains(driver).click_and_hold(on_element=slider).perform()
    # 横向移动鼠标
    webdriver.ActionChains(driver).move_by_offset(xoffset=260, yoffset=0).perform()
    # 松开鼠标
    webdriver.ActionChains(driver).pause(0.5).release().perform()
    driver.switch_to.parent_frame()
except:
    print("没有遇到滑块")
# 登陆按钮: #login-form > div.fm-btn > button
driver.find_element_by_css_selector('#login-form > div.fm-btn > button').click()
time.sleep(2)

place an order

# 2.1 打开抢购页面
driver.get(target_url)
# 2.2 点击立即购买
driver.find_element_by_css_selector('.buybtn').click()
# 2.3 点击观影人
driver.find_element_by_css_selector('.ticket-buyer-select .next-checkbox-label').click()
# 2.4 点击同意并提交
driver.find_element_by_css_selector('.submit-wrapper .next-btn.next-btn-normal.next-btn-medium').click()
time.sleep(2)
 
driver.switch_to.frame(0)
slider2 = driver.find_element_by_css_selector('#nc_1_n1z')
tracks = get_tracks(300)  # 剩下的50%在模拟移动
webdriver.ActionChains(driver).click_and_hold(on_element=slider2).perform()
for x in tracks:
    webdriver.ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform()
else:
    webdriver.ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform()
webdriver.ActionChains(driver).pause(0.5).release().perform()

The JS part is too long, which affects reading, so I won’t post it, just scan the QR code of CSDN’s official certification below on WeChat to get it for free **

About Python Technical Reserve

It is good to learn Python whether it is employment or sideline business to make money, but to learn Python, you still need a study plan. Finally, everyone will share a full set of Python learning materials to help those who want to learn Python!

Click here to get it for free: CSDN spree: "Python learning route & full set of learning materials" free sharing

1. Learning routes in all directions of Python

The technical points in all directions of Python are sorted out to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you can learn more comprehensively.
insert image description here

2. Essential development tools for Python

insert image description here

4. Python video collection

Watching the zero-based learning video is the fastest and most effective way to learn. Following the teacher's ideas in the video, it is still very easy to get started from the basics to the in-depth.
insert image description here

5. Practical cases

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.
insert image description here

6. Python exercises

Check the learning results.
insert image description here

7. Interview materials

We must learn Python to find high-paying jobs. The following interview questions are the latest interview materials from first-line Internet companies such as Ali, Tencent, and Byte, and Ali bosses have given authoritative answers. After finishing this set The interview materials believe that everyone can find a satisfactory job.
insert image description here
insert image description here
This full version of the full set of Python learning materials has been uploaded to CSDN. If you need it, you can save the picture below on your mobile phone and scan the QR code of CSDN's official certification on WeChat to get it for free [ 100% free guarantee]

Guess you like

Origin blog.csdn.net/m0_59162248/article/details/129355264