質問編集履歴
7
様々な場所からコピペしながらプログラムしているのでコメントは気にしないでください。
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -135,6 +135,18 @@ | |
| 135 135 |  | 
| 136 136 | 
             
              r = []
         | 
| 137 137 | 
             
              lis = []
         | 
| 138 | 
            +
             | 
| 139 | 
            +
              
         | 
| 140 | 
            +
              lis  = TABLES.find_all('li')       ← エラーが起きている場所
         | 
| 141 | 
            +
              
         | 
| 142 | 
            +
              for li in lis:  # thead -> liタグを探す
         | 
| 143 | 
            +
                r.append(li.text)  # liタグのテキストを保存
         | 
| 144 | 
            +
                mat.append(r)  # 行をテーブルに保存
         | 
| 145 | 
            +
              
         | 
| 146 | 
            +
              #print(mat[0])
         | 
| 147 | 
            +
              print("matの要素数=", len(mat))
         | 
| 148 | 
            +
             | 
| 149 | 
            +
             | 
| 138 150 | 
             
            ```
         | 
| 139 151 | 
             
             +++++++++++++++++++++++++++++++++
         | 
| 140 152 | 
             
             ↓ここからデータスクレイピング処理
         | 
| @@ -150,4 +162,6 @@ | |
| 150 162 |  | 
| 151 163 | 
             
            ### 補足情報(FW/ツールのバージョンなど)
         | 
| 152 164 |  | 
| 165 | 
            +
            ※様々な場所からコピペしながらプログラムしているのでコメントは間違っているかもしれません、気にしないでください。
         | 
| 166 | 
            +
             | 
| 153 167 | 
             
            Python 3.10.8
         | 
6
TABLES追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -124,6 +124,17 @@ | |
| 124 124 | 
             
              soup = BeautifulSoup(html, "html.parser")
         | 
| 125 125 | 
             
              print(soup.title)
         | 
| 126 126 | 
             
              #print(soup.html)
         | 
| 127 | 
            +
             | 
| 128 | 
            +
              TABLES = []
         | 
| 129 | 
            +
              
         | 
| 130 | 
            +
              TABLES = soup.find('table', {'class': 'results pb'})
         | 
| 131 | 
            +
             | 
| 132 | 
            +
              #len(table)
         | 
| 133 | 
            +
              #print(len)
         | 
| 134 | 
            +
              #print(TABLES)
         | 
| 135 | 
            +
              
         | 
| 136 | 
            +
              r = []
         | 
| 137 | 
            +
              lis = []
         | 
| 127 138 | 
             
            ```
         | 
| 128 139 | 
             
             +++++++++++++++++++++++++++++++++
         | 
| 129 140 | 
             
             ↓ここからデータスクレイピング処理
         | 
5
インデント追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -45,7 +45,8 @@ | |
| 45 45 |  | 
| 46 46 | 
             
            Python 3.10.8
         | 
| 47 47 | 
             
            ------------------------------------------------------------------------------------------
         | 
| 48 | 
            +
            ```python
         | 
| 48 | 
            -
             | 
| 49 | 
            +
            import time
         | 
| 49 50 | 
             
            import random
         | 
| 50 51 | 
             
            import pandas as pd
         | 
| 51 52 | 
             
            from selenium import webdriver
         | 
| @@ -122,7 +123,8 @@ | |
| 122 123 | 
             
              html = driver.page_source.encode('utf-8')
         | 
| 123 124 | 
             
              soup = BeautifulSoup(html, "html.parser")
         | 
| 124 125 | 
             
              print(soup.title)
         | 
| 125 | 
            -
              #print(soup.html) | 
| 126 | 
            +
              #print(soup.html)
         | 
| 127 | 
            +
            ```
         | 
| 126 128 | 
             
             +++++++++++++++++++++++++++++++++
         | 
| 127 129 | 
             
             ↓ここからデータスクレイピング処理
         | 
| 128 130 | 
             
             +++++++++++++++++++++++++++++++++
         | 
4
インデント追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -45,7 +45,7 @@ | |
| 45 45 |  | 
| 46 46 | 
             
            Python 3.10.8
         | 
| 47 47 | 
             
            ------------------------------------------------------------------------------------------
         | 
| 48 | 
            -
            import time
         | 
| 48 | 
            +
            [import time
         | 
| 49 49 | 
             
            import random
         | 
| 50 50 | 
             
            import pandas as pd
         | 
| 51 51 | 
             
            from selenium import webdriver
         | 
| @@ -59,7 +59,7 @@ | |
| 59 59 |  | 
| 60 60 |  | 
| 61 61 |  | 
| 62 | 
            -
            # | 
| 62 | 
            +
            #Powerball history result URL
         | 
| 63 63 | 
             
            powerball_url = 'https://www.usamega.com/powerball/results/' # 1~
         | 
| 64 64 |  | 
| 65 65 | 
             
            num = 1
         | 
| @@ -83,45 +83,46 @@ | |
| 83 83 | 
             
            options.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")
         | 
| 84 84 | 
             
            options.binary_location = "C:\\Program Files\\Google\\Chrome Beta\\Application\\chrome.exe"
         | 
| 85 85 | 
             
            driver = webdriver.Chrome('chromedriver', options=options)
         | 
| 86 | 
            -
            #driver = webdriver.Chrome(executable_path='絶対パス')
         | 
| 87 86 |  | 
| 88 87 |  | 
| 89 88 |  | 
| 90 | 
            -
            while num <= 3:   #うまく動作しないので127はいれていません。
         | 
| 91 89 |  | 
| 92 | 
            -
            <p style="text-indent:2em;">print("URL番号の値")</p>
         | 
| 93 | 
            -
             | 
| 90 | 
            +
            #driver = webdriver.Chrome(executable_path='絶対パス')
         | 
| 91 | 
            +
            while num <= 3:
         | 
| 92 | 
            +
              
         | 
| 93 | 
            +
              print("URL番号の値")
         | 
| 94 | 
            +
              print(num)
         | 
| 94 | 
            -
             | 
| 95 | 
            +
              print("===========================================")
         | 
| 95 96 |  | 
| 96 | 
            -
             | 
| 97 | 
            +
              #当選ページのURL
         | 
| 97 | 
            -
             | 
| 98 | 
            +
              url = powerball_url + str(num)
         | 
| 98 99 |  | 
| 99 | 
            -
             | 
| 100 | 
            +
              print(url)
         | 
| 100 101 |  | 
| 101 | 
            -
             | 
| 102 | 
            +
              num += 1 # 次のページに移動するためにnumに1を追加
         | 
| 102 | 
            -
             | 
| 103 | 
            +
              time.sleep(random.uniform(60, 120)) # 60-120秒Dos攻撃にならないようにするためにコードを止める
         | 
| 103 104 |  | 
| 104 105 | 
             
            '''
         | 
| 105 | 
            -
             | 
| 106 | 
            +
              if num != 2:
         | 
| 106 | 
            -
             | 
| 107 | 
            +
                cookies = pickle.load(open("cookies.pkl", "rb"))
         | 
| 107 | 
            -
             | 
| 108 | 
            +
                for cookie in cookies:
         | 
| 108 | 
            -
             | 
| 109 | 
            +
                  driver.add_cookie(cookie)
         | 
| 109 110 | 
             
            '''
         | 
| 110 111 |  | 
| 111 | 
            -
             | 
| 112 | 
            +
              #該当ページを取得
         | 
| 112 | 
            -
             | 
| 113 | 
            +
              driver.get(url)
         | 
| 113 114 |  | 
| 115 | 
            +
            '''  
         | 
| 116 | 
            +
              if num == 2:
         | 
| 117 | 
            +
                pickle.dump(driver.get_cookies() , open("cookies.pkl","wb"))
         | 
| 114 118 | 
             
            '''
         | 
| 115 | 
            -
            <p style="text-indent:2em;">if num == 2:</p>
         | 
| 116 | 
            -
            <p style="text-indent:4em;">pickle.dump(driver.get_cookies() , open("cookies.pkl","wb"))</p>
         | 
| 117 | 
            -
            '''
         | 
| 118 119 |  | 
| 119 | 
            -
             | 
| 120 | 
            +
                
         | 
| 120 | 
            -
             | 
| 121 | 
            +
              time.sleep(5)
         | 
| 121 | 
            -
             | 
| 122 | 
            +
              html = driver.page_source.encode('utf-8')
         | 
| 122 | 
            -
             | 
| 123 | 
            +
              soup = BeautifulSoup(html, "html.parser")
         | 
| 123 | 
            -
             | 
| 124 | 
            +
              print(soup.title)
         | 
| 124 | 
            -
             | 
| 125 | 
            +
              #print(soup.html)](python)
         | 
| 125 126 | 
             
             +++++++++++++++++++++++++++++++++
         | 
| 126 127 | 
             
             ↓ここからデータスクレイピング処理
         | 
| 127 128 | 
             
             +++++++++++++++++++++++++++++++++
         | 
3
インデント追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -65,6 +65,7 @@ | |
| 65 65 | 
             
            num = 1
         | 
| 66 66 | 
             
            mat = [] # test
         | 
| 67 67 |  | 
| 68 | 
            +
             | 
| 68 69 | 
             
            options = webdriver.ChromeOptions()
         | 
| 69 70 | 
             
            options.add_argument('--headless')
         | 
| 70 71 | 
             
            options.add_argument('--disable-gpu')
         | 
| @@ -88,39 +89,39 @@ | |
| 88 89 |  | 
| 89 90 | 
             
            while num <= 3:   #うまく動作しないので127はいれていません。
         | 
| 90 91 |  | 
| 91 | 
            -
            < | 
| 92 | 
            +
            <p style="text-indent:2em;">print("URL番号の値")</p>
         | 
| 92 | 
            -
            < | 
| 93 | 
            +
            <p style="text-indent:2em;">print(num)</p>
         | 
| 93 | 
            -
            < | 
| 94 | 
            +
            <p style="text-indent:2em;">print("===========================================")</p>
         | 
| 94 95 |  | 
| 95 | 
            -
            < | 
| 96 | 
            +
            <p style="text-indent:2em;"># 当選ページのURL</p>
         | 
| 96 | 
            -
            < | 
| 97 | 
            +
            <p style="text-indent:2em;">url = powerball_url + str(num)</p>
         | 
| 97 98 |  | 
| 98 | 
            -
            < | 
| 99 | 
            +
            <p style="text-indent:2em;">print(url)</p>
         | 
| 99 100 |  | 
| 100 | 
            -
            < | 
| 101 | 
            +
            <p style="text-indent:2em;">num += 1 # 次のページに移動するためにnumに1を追加</p>
         | 
| 101 | 
            -
            < | 
| 102 | 
            +
            <p style="text-indent:2em;">time.sleep(random.uniform(60, 120)) # 60-120秒待ちDOS攻撃防止</p>
         | 
| 102 103 |  | 
| 103 104 | 
             
            '''
         | 
| 104 | 
            -
            < | 
| 105 | 
            +
            <p style="text-indent:2em;">if num != 2:</p>
         | 
| 105 | 
            -
            < | 
| 106 | 
            +
            <p style="text-indent:4em;">cookies = pickle.load(open("cookies.pkl", "rb"))</p>
         | 
| 106 | 
            -
            < | 
| 107 | 
            +
            <p style="text-indent:4em;">for cookie in cookies:</p>
         | 
| 107 | 
            -
            < | 
| 108 | 
            +
            <p style="text-indent:6em;">driver.add_cookie(cookie)</p>
         | 
| 108 109 | 
             
            '''
         | 
| 109 110 |  | 
| 110 | 
            -
            < | 
| 111 | 
            +
            <p style="text-indent:2em;"># 該当ページを取得</p>
         | 
| 111 | 
            -
            < | 
| 112 | 
            +
            <p style="text-indent:2em;">driver.get(url)</p>
         | 
| 112 113 |  | 
| 113 114 | 
             
            '''
         | 
| 114 | 
            -
            < | 
| 115 | 
            +
            <p style="text-indent:2em;">if num == 2:</p>
         | 
| 115 | 
            -
            < | 
| 116 | 
            +
            <p style="text-indent:4em;">pickle.dump(driver.get_cookies() , open("cookies.pkl","wb"))</p>
         | 
| 116 117 | 
             
            '''
         | 
| 117 118 |  | 
| 118 119 |  | 
| 119 | 
            -
            < | 
| 120 | 
            +
            <p style="text-indent:2em;">time.sleep(5)</p>
         | 
| 120 | 
            -
            < | 
| 121 | 
            +
            <p style="text-indent:2em;">html = driver.page_source.encode('utf-8')</p>
         | 
| 121 | 
            -
            < | 
| 122 | 
            +
            <p style="text-indent:2em;">soup = BeautifulSoup(html, "html.parser")</p>
         | 
| 122 | 
            -
            < | 
| 123 | 
            +
            <p style="text-indent:2em;">print(soup.title)</p>
         | 
| 123 | 
            -
            < | 
| 124 | 
            +
            <p style="text-indent:2em;">#print(soup.html)</p>
         | 
| 124 125 | 
             
             +++++++++++++++++++++++++++++++++
         | 
| 125 126 | 
             
             ↓ここからデータスクレイピング処理
         | 
| 126 127 | 
             
             +++++++++++++++++++++++++++++++++
         | 
2
MarkdownでPythonソースにインデントを入れました。
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -44,7 +44,7 @@ | |
| 44 44 | 
             
            デバッグでprintを多用してますがご容赦ください。
         | 
| 45 45 |  | 
| 46 46 | 
             
            Python 3.10.8
         | 
| 47 | 
            -
             | 
| 47 | 
            +
            ------------------------------------------------------------------------------------------
         | 
| 48 48 | 
             
            import time
         | 
| 49 49 | 
             
            import random
         | 
| 50 50 | 
             
            import pandas as pd
         | 
| @@ -58,12 +58,13 @@ | |
| 58 58 | 
             
            import pickle
         | 
| 59 59 |  | 
| 60 60 |  | 
| 61 | 
            +
             | 
| 62 | 
            +
            # Powerball history result URL
         | 
| 61 63 | 
             
            powerball_url = 'https://www.usamega.com/powerball/results/' # 1~
         | 
| 62 64 |  | 
| 63 65 | 
             
            num = 1
         | 
| 64 66 | 
             
            mat = [] # test
         | 
| 65 67 |  | 
| 66 | 
            -
             | 
| 67 68 | 
             
            options = webdriver.ChromeOptions()
         | 
| 68 69 | 
             
            options.add_argument('--headless')
         | 
| 69 70 | 
             
            options.add_argument('--disable-gpu')
         | 
| @@ -81,39 +82,45 @@ | |
| 81 82 | 
             
            options.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")
         | 
| 82 83 | 
             
            options.binary_location = "C:\\Program Files\\Google\\Chrome Beta\\Application\\chrome.exe"
         | 
| 83 84 | 
             
            driver = webdriver.Chrome('chromedriver', options=options)
         | 
| 84 | 
            -
             | 
| 85 85 | 
             
            #driver = webdriver.Chrome(executable_path='絶対パス')
         | 
| 86 | 
            -
            while num <= 3: #うまく動作しないので127はいれていません。
         | 
| 87 86 |  | 
| 88 | 
            -
              print("URL番号の値")
         | 
| 89 | 
            -
              print(num)
         | 
| 90 | 
            -
              print("===========================================")
         | 
| 91 87 |  | 
| 92 | 
            -
              url = powerball_url + str(num)
         | 
| 93 88 |  | 
| 94 | 
            -
              print(url)
         | 
| 95 | 
            -
              
         | 
| 96 | 
            -
              num += 1
         | 
| 97 | 
            -
             | 
| 89 | 
            +
            while num <= 3:   #うまく動作しないので127はいれていません。
         | 
| 98 90 |  | 
| 91 | 
            +
            </></>print("URL番号の値")
         | 
| 92 | 
            +
            </></>print(num)
         | 
| 93 | 
            +
            </></>print("===========================================")
         | 
| 94 | 
            +
             | 
| 95 | 
            +
            </></># 当選ページのURL
         | 
| 96 | 
            +
            </></>url = powerball_url + str(num)
         | 
| 97 | 
            +
             | 
| 98 | 
            +
            </></>print(url)
         | 
| 99 | 
            +
             | 
| 100 | 
            +
            </></>num += 1 # 次のページに移動するためにnumに1を追加
         | 
| 101 | 
            +
            </></>time.sleep(random.uniform(60, 120)) # 60-120秒待ちDOS攻撃防止
         | 
| 102 | 
            +
             | 
| 99 103 | 
             
            '''
         | 
| 100 | 
            -
             | 
| 104 | 
            +
            </></>if num != 2:
         | 
| 101 | 
            -
             | 
| 105 | 
            +
            </></></></>cookies = pickle.load(open("cookies.pkl", "rb"))
         | 
| 102 | 
            -
             | 
| 106 | 
            +
            </></></></>for cookie in cookies:
         | 
| 103 | 
            -
             | 
| 107 | 
            +
            </></></></></></>driver.add_cookie(cookie)
         | 
| 104 108 | 
             
            '''
         | 
| 105 109 |  | 
| 110 | 
            +
            </></># 該当ページを取得
         | 
| 106 | 
            -
             | 
| 111 | 
            +
            </></>driver.get(url)
         | 
| 107 | 
            -
             | 
| 112 | 
            +
             | 
| 108 113 | 
             
            '''
         | 
| 109 | 
            -
             | 
| 114 | 
            +
            </></>if num == 2:
         | 
| 110 | 
            -
             | 
| 115 | 
            +
            </></></></>pickle.dump(driver.get_cookies() , open("cookies.pkl","wb"))
         | 
| 111 116 | 
             
            '''
         | 
| 112 117 |  | 
| 118 | 
            +
             | 
| 119 | 
            +
            </></>time.sleep(5)
         | 
| 113 | 
            -
             | 
| 120 | 
            +
            </></>html = driver.page_source.encode('utf-8')
         | 
| 114 | 
            -
             | 
| 121 | 
            +
            </></>soup = BeautifulSoup(html, "html.parser")
         | 
| 115 | 
            -
             | 
| 122 | 
            +
            </></>print(soup.title)
         | 
| 116 | 
            -
             | 
| 123 | 
            +
            </></>#print(soup.html)
         | 
| 117 124 | 
             
             +++++++++++++++++++++++++++++++++
         | 
| 118 125 | 
             
             ↓ここからデータスクレイピング処理
         | 
| 119 126 | 
             
             +++++++++++++++++++++++++++++++++
         | 
1
表示がおかしかったものを修正
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -18,6 +18,7 @@ | |
| 18 18 | 
             
            2のURLで「Just a moment...」のタイトルでうまくいっていないようです。
         | 
| 19 19 | 
             
            最初は1でも同様のエラーが出力されていましたが、ヘッダーを追加したらうまくいくようになりました。
         | 
| 20 20 |  | 
| 21 | 
            +
            #### エラーメッセージ
         | 
| 21 22 | 
             
            DevTools listening on ws://127.0.0.1:54694/devtools/browser/26d885ba-e1e0-4669-bdfa-78be33bac8ec
         | 
| 22 23 | 
             
            URL番号の値
         | 
| 23 24 | 
             
            1
         | 
| @@ -56,7 +57,7 @@ | |
| 56 57 | 
             
            import json
         | 
| 57 58 | 
             
            import pickle
         | 
| 58 59 |  | 
| 59 | 
            -
             | 
| 60 | 
            +
             | 
| 60 61 | 
             
            powerball_url = 'https://www.usamega.com/powerball/results/' # 1~
         | 
| 61 62 |  | 
| 62 63 | 
             
            num = 1
         | 
| @@ -95,17 +96,20 @@ | |
| 95 96 | 
             
              num += 1
         | 
| 96 97 | 
             
              time.sleep(random.uniform(60, 120)) 
         | 
| 97 98 |  | 
| 99 | 
            +
            '''
         | 
| 98 | 
            -
             | 
| 100 | 
            +
               if num != 2:
         | 
| 99 | 
            -
             | 
| 101 | 
            +
                 cookies = pickle.load(open("cookies.pkl", "rb"))
         | 
| 100 | 
            -
             | 
| 102 | 
            +
                 for cookie in cookies:
         | 
| 101 | 
            -
             | 
| 103 | 
            +
                   driver.add_cookie(cookie)
         | 
| 104 | 
            +
            '''
         | 
| 102 105 |  | 
| 103 106 | 
             
              driver.get(url)
         | 
| 104 107 |  | 
| 108 | 
            +
            '''
         | 
| 105 | 
            -
             | 
| 109 | 
            +
                 if num == 2:
         | 
| 106 | 
            -
             | 
| 110 | 
            +
                   pickle.dump(driver.get_cookies() , open("cookies.pkl","wb"))
         | 
| 111 | 
            +
            '''
         | 
| 107 112 |  | 
| 108 | 
            -
             | 
| 109 113 | 
             
              html = driver.page_source.encode('utf-8')
         | 
| 110 114 | 
             
              soup = BeautifulSoup(html, "html.parser")
         | 
| 111 115 | 
             
              print(soup.title)
         | 
