为什么我的验证码登录总是失败?请高手帮忙
import requestsfrom lxml import etree
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
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'}
r1 = requests.get(url=url, headers=UA).text
x1 = etree.HTML(r1)
p1 = 'https://so.gushiwen.cn' + x1.xpath('//*[@id="imgCode"]/@src')
p2 = requests.get(url=p1, headers=UA).content
with open('d:/a.jpg', 'wb') as fp:
fp.write(p2)
tt = input("请输入验证码:")
url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data = {
'__VIEWSTATE': 'xpoIrDng+GsQMSniKPa22qW3WHG0EkGjBxPg9AK7vhiuPlfVya+4OzfCsJx+8bI23wN7CjYxgBNslsAkBXgu9K3PGrKvsW335s0MIQRr9M8ga1LWIdKvLqjTfEc=',
'__VIEWSTATEGENERATOR': 'C93BE1AE',
'from':'http://so.gushiwen.cn/user/collect.aspx',
'email': '15701168930',
'pwd': 'abcd1234',
'code': tt,
'denglu': '登录'
}
r1 = requests.post(url=url, headers=UA,data=data).text
print(r1)
# <script>alert('提交失败,您输入的验证码有误!');history.back();</script></form>
我是把验证码保存在D盘,然后人工看一下是多少再输入,我也使用其它平台把验证码可以完美识别出来,然后保存在一个变量里,同样的方法登录都是提示验证码错误
试试?
import requests
from lxml import etree
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
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'}
r1 = requests.get(url=url, headers=UA).text
x1 = etree.HTML(r1)
p1 = 'https://so.gushiwen.cn' + x1.xpath('//*[@id="imgCode"]/@src')
p2 = requests.get(url=p1, headers=UA).content
with open('d:/a.jpg', 'wb') as fp:
fp.write(p2)
tt = input("请输入验证码:")
url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data = {
'__VIEWSTATE': '4iCSHrQtwQxDmobngGH/pwd4hlHFpZGPeSebPOdwlwfGBjUgeqJkhcCdcDdGsYG3hcNUHMHxhjjBjis5JlIHoVHJ9JF8t8ap5IZall0jAPRN8/9OmjHlPbSK36k=',
'__VIEWSTATEGENERATOR': 'C93BE1AE',
'from':'http://so.gushiwen.cn/user/collect.aspx',
'email': '15701168930',
'pwd': 'abcd1234',
'code': tt,
'denglu': '登录'
}
r1 = requests.post(url=url, headers=UA,data=data).text
print(r1)
# <script>alert('提交失败,您输入的验证码有误!');history.back();</script></form> Twilight6 发表于 2020-7-25 23:12
试试?
还是提取说 验证码错误 Twilight6 发表于 2020-7-25 23:12
试试?
您刚发的代码好像是和我发的代码一样。{:5_92:} Twilight6 发表于 2020-7-25 23:12
试试?
版主,会不会是这样?
对方服务器返回验证码的时候,会和每个每个信息和验证码进行一次对应,之后我再发POST请求时,服务器又生成了新的验证码 jjxx2005 发表于 2020-7-25 23:22
您刚发的代码好像是和我发的代码一样。
没有 我改了下 VIEWSTATE
对方服务器返回验证码的时候,会和每个每个信息和验证码进行一次对应,之后我再发POST请求时,服务器又生成了新的验证码
嗯 很有可能
__VIEWSTATE/ __VIEWSTATEGENERATOR
这两个参数是动态获取的吧 W011 发表于 2020-7-26 21:02
__VIEWSTATE/ __VIEWSTATEGENERATOR
这两个参数是动态获取的吧
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="YmGFxDIhdJ0mg7e4toD41A70hH82qlJxA2YsViZQwgoGUqBNbKpaPDrcLfTQNjDVsJyEnGW/KWsvjNyiFq2MxeDsQB2x9/MIGcuEkTX+S8fGLo/KeC430RzSKFo=" />
__VIEWSTATE值在网页中需要先将它抓取,然后再post提交 import requests
from lxml import etree
url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx"
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'}
s = requests.session()
r1 = s.get(url=url, headers=UA).text
x1 = etree.HTML(r1)
v_code = x1.xpath('//form[@name="aspnetForm"]/div/input/@value')
p1 = 'https://so.gushiwen.cn' + x1.xpath('//*[@id="imgCode"]/@src')
p2 = s.get(url=p1, headers=UA).content
with open('验证码.jpg', 'wb') as fp:
fp.write(p2)
tt = input("请输入验证码:")
login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data = {
'__VIEWSTATE': v_code,
'__VIEWSTATEGENERATOR': 'C93BE1AE',
'from':'http://so.gushiwen.cn/user/collect.aspx',
'email': '15701168930',
'pwd': 'abcd1234',
'code': tt,
'denglu': '登录'
}
r1 = s.post(url=login_url, headers=UA,data=data).text
url_1 = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
r2 = s.get(url_1,headers = UA)
print(r2.text) guiding 发表于 2020-7-31 10:58
谢谢回复,但仍是不行,他这个网站拿完验证码以后,登录以后,再重新请求url时 就没有上次登录的cookie了。
我用另一种方式操作的
from selenium import webdriver
import requests
from lxml import etree
laqdx = webdriver.Chrome()
laqdx.get("https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx")
username = laqdx.find_element_by_id('email')
username.clear()
username.send_keys('这里输入用户名') # 这里输入自己的网站用户名
PSW = laqdx.find_element_by_id('pwd')
PSW.clear()
PSW.send_keys('这里输入密码') # 这里输入网站密码
a = input("请输入验证码:")
yam = laqdx.find_element_by_id('code')
yam.clear()
yam.send_keys(a)
dl = laqdx.find_element_by_id('denglu')
dl.click()
ewm = laqdx.find_element_by_id('close')
ewm.click()
url = laqdx.current_url
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'}
cookies = laqdx.get_cookies()
cookie_dict = {}
for i in cookies:
cookie_dict] = i["value"]
r1 = requests.get(url=url,headers=UA,cookies=cookie_dict).text
x1 = etree.HTML(r1)
n1 = x1.xpath('//div[@class="sons"]//a//text()')
n1 =[''.join(n1) for i in range(0, len(n1), 2)]
url = x1.xpath('//div[@class="sons"]//a/@href')
url = ['https://so.gushiwen.cn/'+url for url in url]
d1 = dict(zip(n1,url))
for n1,url in d1.items():
print(n1,url)
好吧,我测试时是可以的 guiding 发表于 2020-7-31 11:19
好吧,我测试时是可以的
辛苦加分
页:
[1]