作者Hsins (迅雷不及掩耳盜鈴)
看板Python
標題Re: [問題] 爬蟲取得相對路徑的圖片
時間Tue Dec 14 16:57:11 2021
※ 引述《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
推 sky094315: 不好意思沒有發現要cookie 12/14 18:29
→ sky094315: 感謝您的回覆,這樣我有方向了 12/14 18:29
→ Hsins: 如果你是載來要訓練的話沒差,載來要識別然後登入的話,要 12/14 19:36
→ Hsins: 處理一下 cookies 12/14 19:36
推 sky094315: 好的,謝謝您的回覆 12/14 20:04