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.
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.
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.
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)
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.
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