import requests from lxml import etree import urllib.request url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62', } res = requests.get(url=url,headers=headers) tree = etree.HTML(res.text) # print(tree) viewState = tree.xpath('//input[@id="__VIEWSTATE"]//@value') # print(viewState[0]) viewStateGenerator = tree.xpath('//input[@id="__VIEWSTATEGENERATOR"]//@value') # print(viewStateGenerator[0]) imgCode = tree.xpath('//img[@id="imgCode"]//@src') # print(imgCode[0]) code_url = 'https://so.gushiwen.cn' + imgCode[0] # print(code_url) #urllib.request.urlretrieve(code_url,'code.jpg') 改成通过session获取,保证验证码和点击登录是在同一个请求 session = requests.session() # 都使用session rescode = session.get(code_url) conCode = rescode.content with open('code.jpg','wb')as fp: fp.write(conCode) codeInput = input('请输入你的验证码') login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx' loginData = { '__VIEWSTATE': viewState[0], '__VIEWSTATEGENERATOR': viewStateGenerator[0], 'from': 'http://so.gushiwen.cn/user/collect.aspx', 'email': '[email protected]', 'pwd': 'sgj191026@#', 'code': codeInput, 'denglu': '登录', } loginRes = session.post(url=login_url,data=loginData,headers=headers) loginContent = loginRes.text file = open('loginSuccess.html', 'w',encoding='utf-8') file.write(loginContent)
标签:code,登录,url,gushiwen,二维码,session,print,aspx,古诗词 From: https://www.cnblogs.com/sgj191024/p/17740967.html