作者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://webptt.com/cn.aspx?n=bbs/Python/M.1639472234.A.BE4.html
1F:推 sky094315: 不好意思没有发现要cookie 12/14 18:29
2F:→ sky094315: 感谢您的回覆,这样我有方向了 12/14 18:29
3F:→ Hsins: 如果你是载来要训练的话没差,载来要识别然後登入的话,要 12/14 19:36
4F:→ Hsins: 处理一下 cookies 12/14 19:36
5F:推 sky094315: 好的,谢谢您的回覆 12/14 20:04