鱼C论坛

 找回密码
 立即注册
查看: 1828|回复: 11

为什么我的验证码登录总是失败?请高手帮忙

[复制链接]
发表于 2020-7-25 22:39:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. from lxml import etree
  3. url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
  4. UA = {
  5.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'}
  6. r1 = requests.get(url=url, headers=UA).text
  7. x1 = etree.HTML(r1)
  8. p1 = 'https://so.gushiwen.cn' + x1.xpath('//*[@id="imgCode"]/@src')[0]
  9. p2 = requests.get(url=p1, headers=UA).content
  10. with open('d:/a.jpg', 'wb') as fp:
  11.     fp.write(p2)
  12. tt = input("请输入验证码:")
  13. url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
  14. data = {
  15.     '__VIEWSTATE': 'xpoIrDng+GsQMSniKPa22qW3WHG0EkGjBxPg9AK7vhiuPlfVya+4OzfCsJx+8bI23wN7CjYxgBNslsAkBXgu9K3PGrKvsW335s0MIQRr9M8ga1LWIdKvLqjTfEc=',
  16.     '__VIEWSTATEGENERATOR': 'C93BE1AE',
  17.     'from':'http://so.gushiwen.cn/user/collect.aspx',
  18.     'email': '15701168930',
  19.     'pwd': 'abcd1234',
  20.     'code': tt,
  21.     'denglu': '登录'
  22. }
  23. r1 = requests.post(url=url, headers=UA,data=data).text
  24. print(r1)
  25. # <script>alert('提交失败,您输入的验证码有误!');history.back();</script></form>
复制代码


我是把验证码保存在D盘,然后人工看一下是多少再输入,我也使用其它平台把验证码可以完美识别出来,然后保存在一个变量里,同样的方法登录都是提示验证码错误
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-25 23:12:42 | 显示全部楼层

试试?

  1. import requests
  2. from lxml import etree
  3. url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
  4. UA = {
  5.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'}
  6. r1 = requests.get(url=url, headers=UA).text
  7. x1 = etree.HTML(r1)
  8. p1 = 'https://so.gushiwen.cn' + x1.xpath('//*[@id="imgCode"]/@src')[0]
  9. p2 = requests.get(url=p1, headers=UA).content
  10. with open('d:/a.jpg', 'wb') as fp:
  11.     fp.write(p2)
  12. tt = input("请输入验证码:")
  13. url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
  14. data = {
  15.     '__VIEWSTATE': '4iCSHrQtwQxDmobngGH/pwd4hlHFpZGPeSebPOdwlwfGBjUgeqJkhcCdcDdGsYG3hcNUHMHxhjjBjis5JlIHoVHJ9JF8t8ap5IZall0jAPRN8/9OmjHlPbSK36k=',
  16.     '__VIEWSTATEGENERATOR': 'C93BE1AE',
  17.     'from':'http://so.gushiwen.cn/user/collect.aspx',
  18.     'email': '15701168930',
  19.     'pwd': 'abcd1234',
  20.     'code': tt,
  21.     'denglu': '登录'
  22. }
  23. r1 = requests.post(url=url, headers=UA,data=data).text
  24. print(r1)
  25. # <script>alert('提交失败,您输入的验证码有误!');history.back();</script></form>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-25 23:16:33 | 显示全部楼层

还是提取说 验证码错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-25 23:22:44 | 显示全部楼层

您刚发的代码好像是和我发的代码一样。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-25 23:34:35 | 显示全部楼层

版主,会不会是这样?
对方服务器返回验证码的时候,会和每个每个信息和验证码进行一次对应,之后我再发POST请求时,服务器又生成了新的验证码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-26 07:53:45 | 显示全部楼层
jjxx2005 发表于 2020-7-25 23:22
您刚发的代码好像是和我发的代码一样。


没有 我改了下 VIEWSTATE

对方服务器返回验证码的时候,会和每个每个信息和验证码进行一次对应,之后我再发POST请求时,服务器又生成了新的验证码


嗯 很有可能
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-26 21:02:44 | 显示全部楼层
__VIEWSTATE  /   __VIEWSTATEGENERATOR
这两个参数是动态获取的吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 09:27:55 | 显示全部楼层
W011 发表于 2020-7-26 21:02
__VIEWSTATE  /   __VIEWSTATEGENERATOR
这两个参数是动态获取的吧

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="YmGFxDIhdJ0mg7e4toD41A70hH82qlJxA2YsViZQwgoGUqBNbKpaPDrcLfTQNjDVsJyEnGW/KWsvjNyiFq2MxeDsQB2x9/MIGcuEkTX+S8fGLo/KeC430RzSKFo=" />
__VIEWSTATE值在网页中需要先将它抓取,然后再post提交
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 10:58:07 | 显示全部楼层
  1. import requests
  2. from lxml import etree
  3. url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx"
  4. UA = {
  5.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'}
  6. s = requests.session()
  7. r1 = s.get(url=url, headers=UA).text
  8. x1 = etree.HTML(r1)
  9. v_code = x1.xpath('//form[@name="aspnetForm"]/div[1]/input/@value')[0]
  10. p1 = 'https://so.gushiwen.cn' + x1.xpath('//*[@id="imgCode"]/@src')[0]
  11. p2 = s.get(url=p1, headers=UA).content
  12. with open('验证码.jpg', 'wb') as fp:
  13.     fp.write(p2)
  14. tt = input("请输入验证码:")
  15. login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
  16. data = {
  17.     '__VIEWSTATE': v_code,
  18.     '__VIEWSTATEGENERATOR': 'C93BE1AE',
  19.     'from':'http://so.gushiwen.cn/user/collect.aspx',
  20.     'email': '15701168930',
  21.     'pwd': 'abcd1234',
  22.     'code': tt,
  23.     'denglu': '登录'
  24. }
  25. r1 = s.post(url=login_url, headers=UA,data=data).text
  26. url_1 = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
  27. r2 = s.get(url_1,headers = UA)
  28. print(r2.text)
复制代码

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
jjxx2005 + 1 + 1 + 1

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-31 11:16:05 | 显示全部楼层

谢谢回复,但仍是不行,他这个网站拿完验证码以后,登录以后,再重新请求url时 就没有上次登录的cookie了。
我用另一种方式操作的

  1. from selenium import webdriver
  2. import requests
  3. from lxml import etree
  4. laqdx = webdriver.Chrome()
  5. laqdx.get("https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx")
  6. username = laqdx.find_element_by_id('email')
  7. username.clear()
  8. username.send_keys('这里输入用户名') # 这里输入自己的网站用户名
  9. PSW = laqdx.find_element_by_id('pwd')
  10. PSW.clear()
  11. PSW.send_keys('这里输入密码') # 这里输入网站密码
  12. a = input("请输入验证码:")
  13. yam = laqdx.find_element_by_id('code')
  14. yam.clear()
  15. yam.send_keys(a)
  16. dl = laqdx.find_element_by_id('denglu')
  17. dl.click()
  18. ewm = laqdx.find_element_by_id('close')
  19. ewm.click()
  20. url = laqdx.current_url
  21. UA = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'}
  22. cookies = laqdx.get_cookies()
  23. cookie_dict = {}
  24. for i in cookies:
  25.     cookie_dict[i["name"]] = i["value"]
  26. r1 = requests.get(url=url,headers=UA,cookies=cookie_dict).text
  27. x1 = etree.HTML(r1)
  28. n1 = x1.xpath('//div[@class="sons"]//a//text()')
  29. n1 =[''.join(n1[i:i+2]) for i in range(0, len(n1), 2)]
  30. url = x1.xpath('//div[@class="sons"]//a/@href')
  31. url = ['https://so.gushiwen.cn/'+url for url in url]
  32. d1 = dict(zip(n1,url))
  33. for n1,url in d1.items():
  34.     print(n1,url)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-31 11:19:16 | 显示全部楼层
好吧,我测试时是可以的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-31 11:29:55 | 显示全部楼层
guiding 发表于 2020-7-31 11:19
好吧,我测试时是可以的

辛苦加分
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 14:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表