こんにちは、ミナピピン(@python_mllover)です!
今回は案件でプレステージのサンプル動画のMP4ファイル名を特定したかったので、Seleniu㎡でリクエストをとなした時の中身を確認する方法を探していたところ以下のような感じで確認することができましたのでブログにメモっておきます。
Seleniumでネットワーク情報を取得する
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import chromedriver_binary
import json
url = 'https://www.mgstage.com/'
d = DesiredCapabilities.CHROME
d['goog:loggingPrefs'] = { 'performance': 'ALL' }
driver = webdriver.Chrome(ChromeDriverManager().install(), desired_capabilities=d)
driver.get(url)
# 認証ボタンを押す
try:
driver.find_element_by_xpath('//*[@id="AC"]').click()
except:
pass
# ネットワーク情報で向こうが送ってきたファイルのURLだけを抽出する
for entry_json in driver.get_log('performance'):
entry = json.loads(entry_json['message'])
if entry['message']['method'] != 'Network.requestWillBeSent' :
continue
print(entry['message']['params']['request']['url'])
driver.close()
<実行結果>
https://www.mgstage.com/ https://static.mgstage.com/mgs/css/pc/style.css https://static.mgstage.com/mgs/script/pc/jquery.min.jshttps://static.mgstage.com/mgs/img/pc/18sp_banner_mgs.jpg https://static.mgstage.com/mgs/script/pc/jquery.cookie.js https://static.mgstage.com/mgs/css/pc/default.css https://static.mgstage.com/mgs/css/pc/base.css https://static.mgstage.com/mgs/css/pc/common.css https://static.mgstage.com/mgs/css/pc/content.css https://static.mgstage.com/mgs/css/pc/clearfix.css https://static.mgstage.com/mgs/css/pc/reviewstar.css https://static.mgstage.com/mgs/css/pc/icon.css https://static.mgstage.com/mgs/css/pc/ranking_monthly.csshttps://static.mgstage.com/mgs/img/pc/mgs_header_bold.gif https://static.mgstage.com/mgs/img/pc/logo_mgs.gif https://static.mgstage.com/mgs/img/pc/r18_90_90.gif https://static.mgstage.com/mgs/img/pc/footer_border.gif https://static.mgstage.com/mgs/css/pc/cart.csshttps://image.mgstage.com/images/prestigepremium/300mium/700/pf_t1_300mium-700.jpg ~~~~~ https://static.mgstage.com/mgs/favicon.ico
こんな感じでJSやCSSや画像ファイルのPATHが取得できます。
参考:https://blog.motikan2010.com/entry/2019/11/20/%E3%80%90Python%E3%80%91Selenium%E5%81%B4%E3%81%8B%E3%82%89%E3%83%8D%E3%83%83%E3%83%88%E3%83%AF%E3%83%BC%E3%82%AF%E6%83%85%E5%A0%B1%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B

コメント