1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonメモ : 某くじの自動化

Posted at

某くじの自動化、以下を参考に。自分への備忘録

https://qiita.com/Tatsuya-code/items/b5140f24c5fb611060c2

Chrome、Edge いずれでも実行できるようにする

selenium

pip install selenium

ドライバー

chrome

https://chromedriver.chromium.org/downloads

以下のように置いています。

C:\Users\あなたのユーザー名が来るかな\Documents\ドライバー\chromedriver.exe

Edge

https://developer.microsoft.com/ja-jp/microsoft-edge/tools/webdriver/?form=MA13LH&ch=1#downloads

以下のように置いています。

C:\Users\あなたのユーザー名が来るかな\Desktop\Python\ドライバー\msedgedriver.exe

chrome python

chromeで事前ログインする。そして、以下をコードに記載しログイン状態を引き継ぐ

--user-data-dir=C:\Users\あなたのユーザー名が来るかな\AppData\Local\Google\Chrome\User Data'

  • コード
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time

# Chromeオプションの設定
options = Options()
options.add_argument('--disable-gpu')
options.add_argument('--disable-extensions')
options.add_argument('--proxy-server="direct://"')
options.add_argument('--proxy-bypass-list=*')
options.add_argument('--no-sandbox')
options.add_argument('--lang=ja')
options.add_argument('--user-data-dir=C:\\Users\\あなたのユーザー名が来るかな\\AppData\\Local\\Google\\Chrome\\User Data')


# Chromeドライバーのパスを指定
driver_path = "C:\\Users\\あなたのユーザー名が来るかな\\Documents\\ドライバー\\chromedriver.exe"

# WebDriverサービスの設定
service = Service(executable_path=driver_path)

# Chromeドライバーの起動
driver = webdriver.Chrome(service=service, options=options)

# くじ一覧ページにアクセス
driver.get("https://rakucoin.appspot.com/rakuten/kuji/")

# くじのURLを取得
urls = driver.find_elements("xpath", "//table/tbody/tr/td/a")
url_txts = [url.get_attribute("href") for url in urls]

# くじを引く処理
for url in url_txts:
    driver.get(url)
    print("Accessing " + url)
    if len(driver.find_elements("id", "entry")) > 0:
        print("success")
        time.sleep(2)
        start_button = driver.find_element("id", "entry")
        start_button.click()
        time.sleep(20)
    else:
        print("failed")
        time.sleep(2)

# ブラウザを閉じる
driver.close()
driver.quit()

Edge python

Edgeで事前ログインする。そして、以下をコードに記載しログイン状態を引き継ぐ

options.add_argument(r'--user-data-dir=C:\Users\あなたのユーザー名が来るかな\AppData\Local\Microsoft\Edge\User Data')  
# ユーザーデータディレクトリのパス
options.add_argument('--profile-directory=Profile 2')  
# 使用するプロファイルディレクトリを指定

Profileは使うやつを記載

  • コード
import os

# Microsoft Edgeのプロセスを終了させる
os.system("taskkill /F /IM msedge.exe")

私は、上のようにいちいち消さないとうまく行かなかった。。

そして、以下

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
import time

# Edgeオプションの設定
options = Options()
options.use_chromium = True  # ChromiumベースのEdgeを使用することを明示
options.add_argument('--disable-gpu')  # GPUハードウェアアクセラレーションを無効にする
options.add_argument('--disable-extensions')  # 拡張機能を無効にする
options.add_argument('--proxy-server="direct://"')  # プロキシサーバーを無効にする
options.add_argument('--proxy-bypass-list=*')  # すべてのアドレスでプロキシをバイパス
options.add_argument('--no-sandbox')  # サンドボックスモードを無効にする
options.add_argument('--lang=ja')  # 言語を日本語に設定
options.add_argument(r'--user-data-dir=C:\Users\あなたのユーザー名が来るかな\AppData\Local\Microsoft\Edge\User Data')  
# ユーザーデータディレクトリのパス
options.add_argument('--profile-directory=Profile 2')  # 使用するプロファイルディレクトリを指定

# Edgeドライバーのパスを指定
driver_path = r"C:\Users\あなたのユーザー名が来るかな\Desktop\Python\ドライバー\msedgedriver.exe"

# WebDriverサービスの設定
service = Service(executable_path=driver_path)

# Edgeドライバーの起動
driver = webdriver.Edge(service=service, options=options)

# ウェブサイトにアクセス
driver.get("https://rakucoin.appspot.com/rakuten/kuji/")

# くじのURLを取得
urls = driver.find_elements(By.XPATH, "//table/tbody/tr/td/a")
url_txts = [url.get_attribute("href") for url in urls]

# くじを引く処理
for url in url_txts:
    driver.get(url)
    print("Accessing " + url)
    if len(driver.find_elements(By.ID, "entry")) > 0:
        print("success")
        time.sleep(2)
        start_button = driver.find_element(By.ID, "entry")
        start_button.click()
        # 必要に応じて処理を追加
        time.sleep(20)  # 処理が完了するまで待機
    else:
        print("failed")
        time.sleep(2)

# ユーザーの入力を待つ(任意)
input("結果を確認した後、何かキーを押してください...")

# ブラウザを閉じる
driver.close()
driver.quit()

という感じで、うまく行った。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?