为什么此web图像url只在浏览器中有效一次?

2024-04-20 11:59:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个python脚本来自动renew fastssh account。在续订页面上,有一个如下结构的验证码:

<label>
<img src='https://fastssh.com/images/temp/blablablabla1.jpg' >
X
<img src='https://fastssh.com/images/temp/blablablabla2.jpg' >
=
</label>
<input type="text" name="CaptchaPass" id="captcha"  required/>

所以我需要从两个jpg中确定两个数,然后做算术。为了自动化这个过程,我尝试用以下代码获取jpg

 import requests, sys
 from bs4 import BeautifulSoup

 url = 'https://www.fastssh.com/page/renew-ssh-account'
 s = requests.session()
 text = s.get(url).text
 soup = BeautifulSoup(text)

 options = soup.find_all('option')
 found = False
 for option in options:
     if 'fr.serverip.co' in ''.join(option.contents):
         serverid = option['value']
         found = True
         break
 if not found:
     sys.exit('server not found.')

 captcha = soup.find(id='captcha')
 imgTag2 = captcha.find_previous('img')
 img2 = s.get(imgTag2['src'], stream=True)

 with open('num2.jpg', 'wb') as out:
     for block in img2.iter_content(1024):
         if not block:
             break
         out.write(block)

然而,最后写入磁盘的num2.jpg实际上是一个html文件!你知道吗

我还发现,如果我使用我的chrome浏览器来加载renew page,手动将jpgurl从html源代码复制并粘贴到一个新的选项卡,然后按enter键,它实际上会将我重定向到主页,我相信这就是我在num2.jpg上面得到的。你知道吗

我想这可能和饼干有关?但是requests包中的会话不应该处理所有必需的cookie吗?你知道吗


Tags: textinhttpssrccomimgfindrequests