Python 为什么此web图像url仅在浏览器中有效一次?
我正在尝试编写一个python脚本来自动执行。在续订页面上,有一个如下结构的验证码:Python 为什么此web图像url仅在浏览器中有效一次?,python,cookies,python-requests,Python,Cookies,Python Requests,我正在尝试编写一个python脚本来自动执行。在续订页面上,有一个如下结构的验证码: <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="CaptchaP
<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/>
然而,最后写入磁盘的num2.jpg实际上是一个html文件
我还发现,如果我使用我的chrome浏览器加载,手动将jpg url从html源代码复制粘贴到一个新选项卡,然后点击enter,它实际上会将我重定向到主页,我相信这就是我在上面的num2.jpg中得到的
我想这可能和饼干有关?但是请求包中的会话不应该处理所有必要的cookie吗?是的,请求处理cookie。所以你的诊断是不正确的,不仅仅是cookies。@MartijnPieters如果我使用Chrome的开发工具,我可以看到另外两个cookies,除了PHPESSID,我在请求中看不到它们。这两个是_ga和_gat。如果我使用FireFox,我可以看到asc和auc,这是我使用Chrome浏览器都看不到的。考虑到这些cookie可能是由另一个页面设置的,或者是在过去的某个时候设置的。使用私密会话匿名、私密浏览,无论特定浏览器的功能是什么,都可以在没有Cookie的情况下启动,并查找设置的Cookie头。但也可以查看其他头。例如,可能是服务器正在解析用户代理。服务器是一个黑匣子,我们无法知道它的内部工作,但您可以在这里观察输入。只需考虑所有可能的输入。
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)