Get a ticket for a theme park with python

background

A theme park that requires reservations for tickets due to the recent spread of infectious diseases! Now I can't get my dream passport without staring at my computer or smartphone all day and hitting the URL repeatedly.

I thought it was a good opportunity to practice scraping, which was frustrated in the past, so I tried to use scraping to get a passport as easily as possible.

What to do

You can see how difficult it is to get a passport for the land of dreams on the here site.

The first barrier is that the URL that appears on the Internet jumps to the page that the current page cannot be opened due to excessive access.

First of all, the goal of this time is to get over this, and implement a program that accesses this URL every second and contacts you because it is easy to connect to your e-mail address when you can access it.

environment

Run on python3. All dependent libraries should be installable with pip3, so if you haven't installed it yet

$pip3 install hoge

The environment should be ready.

Implementation

main.py


import requests
from bs4 import BeautifulSoup
import time
import gmail
for i in range(100000):
	urlName = "https://reserve.tokyodisneyresort.jp/ticket/search/"
	url = requests.get(urlName)
	soup = BeautifulSoup(url.content, "html.parser")
	title = soup.find("h1").text
	print(i)
	if(title!="Notice from Tokyo Disney Resort"):
		print("SUCEED!")
		gmail.send()
		print(soup)#All information output
		break
	print("FAILURE")
	time.sleep(1)

Since gmail is a script described later, no install is required.

Check the flow here. First, access the link in a long or infinite loop, and if the title is "Notice from a certain theme park", access fails and try again. If it is another title, access is successful, so send gmail. It spits out all the HTML information of the site and ends.

Here, we want to fully automate in the future, so HTML information is output as standard.

Next, write a script to send gmail.

gmail.py


import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate

def send():
	FROM_ADRESS = "The email address of the sender"
	PASSWORD = "Password of the sender of the email"
	TO_ADRESS = "Email recipient's email address"
	smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
	smtpobj.ehlo()
	smtpobj.starttls()
	smtpobj.ehlo()
	smtpobj.login(FROM_ADRESS, PASSWORD)

	#Describe the content of the email
	msg = MIMEText('https://reserve.tokyodisneyresort.jp/ticket/search/')
	msg['Subject'] = 'EASY TO ACCESS NOW!'
	msg['From'] = FROM_ADRESS
	msg['To'] = TO_ADRESS
	msg['Date'] = formatdate()
	smtpobj.sendmail(FROM_ADRESS,TO_ADRESS,msg.as_string())
	smtpobj.close()

Implemented so that the URL for purchasing a ticket can be sent when send () is read with the minimum required code. One thing to keep in mind here is that gmail.send () will not work unless the sender of the email allows access from less secure apps in Gmail's security settings. You have to change the setting to allow before moving it (it is not allowed by default)

How to move

If you copy and paste all the code and save it in the same directory as main.py, gmail.py

$python3 main.py

Should be executed in.

from now on

I would like to study scraping with python with the goal of making purchases fully automatic using the HTML information obtained by this program.

Recommended Posts

Get a ticket for a theme park with python
Get a token for conoha in python
[Python] Get the files in a folder with Python
Create a LINE BOT with Minette for Python
Procedure for creating a LineBot made with Python
Commands for creating a python3 environment with virtualenv
Get date with python
A layman wants to get started with Python
Get a quick Python development environment with Poetry
[Introduction to Python] How to get the index of data with a for statement
Get financial data with python (then a little tinkering)
Build a python environment for each directory with pyenv-virtualenv
Register a ticket with redmine API using python requests
Create a Layer for AWS Lambda Python with Docker
Python: Get a list of methods for an object
Get Twitter timeline with python
Get Youtube data with python
Make a fortune with Python
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Get stock price with Python
Create a directory with python
Get home directory with python
Get keyboard events with python
Get Alembic information with Python
Recommendations for django, wagtail ~ Why develop a website with python ~
Create a child account for connect with Stripe in Python
Experiment to make a self-catering PDF for Kindle with Python
Installation procedure for Python and Ansible with a specific version
Get a list of purchased DMM eBooks with Python + Selenium
A sample for drawing points with PIL (Python Imaging Library).
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Get a clean Python development environment with pyenv + pipx + Poetry
Get data from analytics API with Google API Client for python
Library for specifying a name server and dig with python
Turn an array of strings with a for statement (Python3)
[Python] Create a screen for HTTP status code 403/404/500 with Django
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Get reviews with python googlemap api
Let's make a GUI with python.
Solve ABC166 A ~ D with Python
Getting Started with Python for PHPer-Classes
Get the weather with Python requests
Create a virtual environment with Python!
Get web screen capture with python
Get the weather with Python requests 2
[Python] Get economic data with DataReader
I made a fortune with Python.
Building a virtual environment with Python 3
How to get started with Python
Solve ABC168 A ~ C with Python
Make a recommender system with python
[Small story] Get timestamp with Python
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Get Qiita trends with Python scraping