Re: [問題] 爬蟲取得相對路徑的圖片

看板Python作者 (迅雷不及掩耳盜鈴)時間3年前 (2021/12/14 16:57), 編輯推噓2(203)
留言5則, 2人參與, 3年前最新討論串2/2 (看更多)
※ 引述《sky094315 (monkeyo)》之銘言: : 想請問一下各位大大 : 目前正在做一個網站爬蟲,此網站會有圖形驗證碼,而此驗證碼每次重新整理後都會改變 : (伺服器端會產生亂數製作一組圖片),且只可取得一次。 : 請問有其他不使用selenium開啟瀏覽器把圖檔抓下來的方法嗎? : 或是有什麼關鍵字呢? : 謝謝 : 參考資料:https://weirenxue.github.io/2021/07/04/python_selenium_captcha/ : 這邊附上 : 參考網站:https://aaav2.hinet.net/A1/AuthScreen.jsp 你這參考網站沒 cookies 進不去 所以我拿其他頁面的內容示範下: https://aaaservice.hinet.net/User/unipresidentConsole.jsp https://aaacp.hinet.net/CP/index.html 這兩個頁面都有 Captcha, 透過 Chrome/Edge 的開發者工具可以檢查: https://i.imgur.com/V5x4d9u.png
其中的 Captcha 主要是透過向以下兩個 URI 打 GET 獲取 https://aaaservice.hinet.net/User/Captcha?rdn=1639470286847 https://aaacp.hinet.net/CP/Captcha?rdn=1639469984177 其中後面的 rdn 一臉就長得很像 timestamp 餵過去 https://www.epochconverter.com/ 檢查下是含 milliseconds 的 所以事情就變得很簡單了: 1. 打請求 2. 存圖片 ```python import requests from datetime import datetime for _ in range(10): current_timestamp = round(datetime.now().timestamp() * 1000) image_url = f"https://aaacp.hinet.net/CP/Captcha?rdn={current_timestamp}" image_data = requests.get(image_url).content with open(f'./{current_timestamp}.jpg', 'wb') as handler: handler.write(image_data) ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.83.229.56 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1639472234.A.BE4.html

12/14 18:29, 3年前 , 1F
不好意思沒有發現要cookie
12/14 18:29, 1F

12/14 18:29, 3年前 , 2F
感謝您的回覆,這樣我有方向了
12/14 18:29, 2F

12/14 19:36, 3年前 , 3F
如果你是載來要訓練的話沒差,載來要識別然後登入的話,要
12/14 19:36, 3F

12/14 19:36, 3年前 , 4F
處理一下 cookies
12/14 19:36, 4F

12/14 20:04, 3年前 , 5F
好的,謝謝您的回覆
12/14 20:04, 5F
文章代碼(AID): #1Xk5ngla (Python)
討論串 (同標題文章)
文章代碼(AID): #1Xk5ngla (Python)