Skip to content

Instantly share code, notes, and snippets.

@jshirius
Created March 2, 2020 12:25
Show Gist options
  • Save jshirius/32162d63e5a31e0f369f449a01f65bae to your computer and use it in GitHub Desktop.
Save jshirius/32162d63e5a31e0f369f449a01f65bae to your computer and use it in GitHub Desktop.
seleniumを使ってjavascriptによる非同期読み込み、iframe対策が必要になるサイトのログイン方法
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# seleniumを使ってASAにログインするサンプル\n",
"\n",
"## 概要\n",
"・seleniumを使ってASAに自動ログインするサンプルプログラム<br>\n",
"・ASAを例にとっている理由は、スクレイピングにおいて、ちょっと複雑なログインフローを紹介するため <br>\n",
"・実際にASAに対して、スクレイピングで負荷をかけ過ぎるとアカウントごとBANされる可能性があるので、あくまで自己責任で<br>\n",
"・ちょっと複雑とは、ASAでは、javascriptによる非同期読み込み、iframe対策が必要になる。今後、他のサイトでも発生するフローになるため記載<br>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#ASAにログインする\n",
"#seleniumのドライバーとしてchromedriverを使用する\n",
"\n",
"from selenium import webdriver\n",
"from selenium.webdriver.common.by import By\n",
"from selenium.webdriver.support.ui import WebDriverWait\n",
"from selenium.webdriver.support import expected_conditions\n",
"import time\n",
"\n",
"#ヘッドレスモードで appleのsearchadsのログインを試みる\n",
"#「user-agent」を設定しないと、サイトの情報が返ってこないので注意\n",
"#この例では、適当な「user-agent」を設定しているので、適切に変更する必要がある。\n",
"options = webdriver.ChromeOptions()\n",
"options.add_argument('--headless')\n",
"options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0 Mobile/14C92 Safari/602.1')\n",
"\n",
"#chromedriverのpathを指定する(事前に、バージョンに注意してchromedriverをダウンロードしておくこと)\n",
"driver = webdriver.Chrome('./chromedriver', options = options) \n",
"\n",
"driver.get('https://app.searchads.apple.com/cm/app')\n",
"driver.save_screenshot('search_results1.png')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(driver.title) #成功すると「Sign In - Apple」と表示される\n",
"print(driver.current_url) #現在のURLを出力する"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#ASAの場合、iframeの切り替えが必要\n",
"iframe = driver.find_element_by_css_selector('iframe#aid-auth-widget-iFrame')\n",
"driver.switch_to_frame(iframe)\n",
"\n",
"#要素「account_name_text_field」が表示されるまで、最大30秒の待ち\n",
"wait = WebDriverWait(driver, 30)\n",
"element = wait.until(expected_conditions.visibility_of_element_located((By.ID, \"account_name_text_field\")))\n",
"\n",
"#IDを入力してボタンをクリック\n",
"driver.find_element_by_id('account_name_text_field').send_keys(\"Your Apple ID\")\n",
"driver.find_element_by_id('sign-in').click()\n",
"\n",
"#デバッグとしてブラウザーのスクリーンショットを取る\n",
"driver.save_screenshot('search_results1.png')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#要素「password_text_field」が表示されるまで、最大30秒の待ち\n",
"wait = WebDriverWait(driver, 30)\n",
"element = wait.until(expected_conditions.visibility_of_element_located((By.ID, \"password_text_field\")))\n",
"\n",
"#パスワードを入力するところ\n",
"driver.find_element_by_id('password_text_field').send_keys(\"Your Password\")\n",
"\n",
"wait = WebDriverWait(driver, 10)\n",
"element = wait.until(expected_conditions.visibility_of_element_located((By.ID, \"sign-in\")))\n",
"driver.find_element_by_id('sign-in').click()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(driver.title)\n",
"print(driver.current_url)\n",
"\n",
"#デバッグ的に画像に保存する\n",
"driver.save_screenshot('search_results2.png')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#終了\n",
"driver.quit()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment