问题:pycharm下载的图片显示image not loaded
我是做的爬虫绕过验证码登录,问题出在下载验证码的图片,需要在代码运行中查看下载的图片,然后手动输入验证码,代码才能继续运行,我试过将代码stop,然后在本地将代码转换成png格式或其他图片格式,再次运行之前的验证码图片可以显示了,但验证码是每次运行代码就刷新的,更改格式后的验证码图片相当于上一次的验证码
以下是我的代码实现
url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.78'
}
response = requests.get(url=url,headers=headers)
content = response.text
soup = BeautifulSoup(content,'lxml')
viewstate = soup.select('#__VIEWSTATE')[0].attrs.get('value')
viewstategenerator = soup.select('#__VIEWSTATEGENERATOR')[0].attrs.get('value')
code = soup.select('#imgCode')[0].attrs.get('src')
code_url = 'https://so.gushiwen.cn' + code
session = requests.Session()
response_code = session.get(code_url)
content_code = response_code.content
fp = open('code.jpg','wb')
fp.write(content_code)
code_name = input('请输入你的验证码')
url_post = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data_post = {
'__VIEWSTATE': viewstate,
'__VIEWSTATEGENERATOR': viewstategenerator,
'from': 'http://so.gushiwen.cn/user/collect.aspx',
'email': '2449574020@qq.com',
'pwd': 'zhangyunqi',
'code': code_name,
'denglu': '登录'
}
response_post = session.post(url=url_post,data=data_post,headers=headers)
content_post = response_post.text
fp = open('gushiwen.html','w',encoding='utf-8')
fp.write(content_post)
为了便于各位读懂我的我的代码,思路和注释可以参考:https://blog.csdn.net/qq_47431008/article/details/128923323?spm=1001.2014.3001.5501
希望各位英杰能提供一些解决方案,万分感谢