前提
Pythonでseleniumを利用してデータのスクレイピングを実施しています。
https://www.usamega.com/powerball/results/1 ←この数字
最後の数字はカウントアップしていき、現在は126までホームページが存在します。
最初の1は問題なく取得できるのですが、whileで回して2以降でエラーになります。
実現したいこと
2以降のスクレイピングを実施するための手段を教えていただけませんでしょう。
発生している問題・エラーメッセージ
2のURLで「Just a moment...」のタイトルでうまくいっていないようです。
最初は1でも同様のエラーが出力されていましたが、ヘッダーを追加したらうまくいくようになりました。
エラーメッセージ
DevTools listening on ws://127.0.0.1:54694/devtools/browser/26d885ba-e1e0-4669-bdfa-78be33bac8ec
URL番号の値
1
https://www.usamega.com/powerball/results/1
<title>Powerball Past Results | USA Mega</title> matの要素数= 338 1回終わり----------------------------------------------------- URL番号の値 2 =========================================== https://www.usamega.com/powerball/results/2 <title>Just a moment...</title> Traceback (most recent call last): File "C:\Users\user02\loto6\powerball_web2.py", line 89, in <module> lis = TABLES.find_all('li') AttributeError: 'NoneType' object has no attribute 'find_all'該当のソースコード
デバッグでprintを多用してますがご容赦ください。
Python 3.10.8
python
1import time 2import random 3import pandas as pd 4from selenium import webdriver 5from selenium.webdriver.chrome.options import Options 6from selenium.webdriver.common.by import By 7from bs4 import BeautifulSoup 8import chromedriver_binary 9import numpy as np 10import json 11import pickle 12 13 14 15#Powerball history result URL 16powerball_url = 'https://www.usamega.com/powerball/results/' # 1~ 17 18num = 1 19mat = [] # test 20 21 22options = webdriver.ChromeOptions() 23options.add_argument('--headless') 24options.add_argument('--disable-gpu') 25options.add_argument('--no-sandbox') 26options.add_argument('--ignore-certificate-errors') 27options.add_argument('--allow-running-insecure-content') 28options.add_argument('--disable-web-security') 29options.add_argument('--disable-desktop-notifications') 30options.add_argument("--disable-extensions") 31options.add_argument('--lang=en') 32options.add_argument('--blink-settings=imagesEnabled=false') 33options.add_argument('--log-level=1') 34options.add_argument("--disable-blink-features=AutomationControlled") 35#headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'} 36options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36") 37options.binary_location = "C:\\Program Files\\Google\\Chrome Beta\\Application\\chrome.exe" 38driver = webdriver.Chrome('chromedriver', options=options) 39 40 41 42 43#driver = webdriver.Chrome(executable_path='絶対パス') 44while num <= 3: 45 46 print("URL番号の値") 47 print(num) 48 print("===========================================") 49 50 #当選ページのURL 51 url = powerball_url + str(num) 52 53 print(url) 54 55 num += 1 # 次のページに移動するためにnumに1を追加 56 time.sleep(random.uniform(60, 120)) # 60-120秒Dos攻撃にならないようにするためにコードを止める 57 58''' 59 if num != 2: 60 cookies = pickle.load(open("cookies.pkl", "rb")) 61 for cookie in cookies: 62 driver.add_cookie(cookie) 63''' 64 65 #該当ページを取得 66 driver.get(url) 67 68''' 69 if num == 2: 70 pickle.dump(driver.get_cookies() , open("cookies.pkl","wb")) 71''' 72 73 74 time.sleep(5) 75 html = driver.page_source.encode('utf-8') 76 soup = BeautifulSoup(html, "html.parser") 77 print(soup.title) 78 #print(soup.html) 79 80 TABLES = [] 81 82 TABLES = soup.find('table', {'class': 'results pb'}) 83 84 #len(table) 85 #print(len) 86 #print(TABLES) 87 88 r = [] 89 lis = [] 90 91 92 lis = TABLES.find_all('li') ← エラーが起きている場所 93 94 for li in lis: # thead -> liタグを探す 95 r.append(li.text) # liタグのテキストを保存 96 mat.append(r) # 行をテーブルに保存 97 98 #print(mat[0]) 99 print("matの要素数=", len(mat)) 100 101
+++++++++++++++++++++++++++++++++
↓ここからデータスクレイピング処理
+++++++++++++++++++++++++++++++++
試したこと
スリープを多めにとったり、Cookieを保存、読み出してリトライしましたがうまく動作しません。
補足情報(FW/ツールのバージョンなど)
※様々な場所からコピペしながらプログラムしているのでコメントは間違っているかもしれません、気にしないでください。
Python 3.10.8

回答1件
あなたの回答
tips
プレビュー