网页爬虫 - python requests库模拟登陆学校教务网遇到数据库繁忙

查看:107
本文介绍了网页爬虫 - python requests库模拟登陆学校教务网遇到数据库繁忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

第一次写爬虫也是第一次在SF提问题=。=
思路就是get登陆下网址获得一个cookie然后带着cookie以post方式登陆
headers伪造和浏览器一模一样
服务器总是返回一个数据库繁忙的界面,找了很久也不知道原因在哪


s = requests.Session()
x = s.get(loginUrl)
r = s.post(postUrl,data=
{
'zjh':'********',
'mm':'********',
'v_yzm':CodeRecognition()
}
,headers = headers)


print(x.headers)
print(x.request.headers)
print(r.headers)
print(r.request.headers)


f=file("cookie.txt","w+")
f.write(r.text)
f.close()

CodeRecognition() 这个验证码识别模块源码太长就不贴出来了,就是先用urllib库访问验证码地址 http://222.195.242.222:8080/validateCodeAction.do 把图片下载到本地,然后对图像二值化灰度等处理后调用Tesseract-OCR识别并返回

补充内容:在chrome中用EditThisCookie管理工具删掉网站的cookie然后刷新页面,登录会失败,返回数据库繁忙。但是我用session先访问网站返回给了我一个setcookie,然后带着这个cookie post表单为什么还是会有这个错误呢?

这是在sublime3中运行打印出的内容:

{'Transfer-Encoding': 'chunked', 'Set-Cookie': 'JSESSIONID=bhazvcnoUA-YYbw_WQZsv; path=/', 'Keep-Alive': 'timeout=8, max=500', 'Server': 'Apache', 'Connection': 'Keep-Alive', 'Cache-Control': 'private', 'Date': 'Sun, 15 May 2016 09:06:53 GMT', 'Content-Type': 'text/html; charset=GBK'}

{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.9.1'}

{'Transfer-Encoding': 'chunked', 'Keep-Alive': 'timeout=8, max=499', 'Server': 'Apache', 'Connection': 'Keep-Alive', 'Date': 'Sun, 15 May 2016 09:06:53 GMT', 'Content-Type': 'text/html; charset=GBK'}

{'Origin': 'http://222.195.242.222:8080', 'Content-Length': '77', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive', 'Accept': '*/*', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36', 'Host': '222.195.242.222:8080', 'Referer': 'http://222.195.242.222:8080/', 'Cookie': 'JSESSIONID=bhazvcnoUA-YYbw_WQZsv', 'Content-Type': 'application/x-www-form-urlencoded'}

解决方案

import requests

cookies = {}

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/50.0.2661.86 Safari/537.36'
}


def get_code():
    url = 'http://222.195.242.222:8080/validateCodeAction.do'
    resp = requests.get(url, headers=headers)
    cookies['JSESSIONID'] = resp.cookies.get('JSESSIONID')
    with open('code.jpg', 'wb') as img:
        img.write(resp.content)


def login(username, password, code):
    url = 'http://222.195.242.222:8080/loginAction.do'
    form = {
        'zjh1': '',
        'tips': '',
        'lx': '',
        'evalue': '',
        'eflag': '',
        'fs': '',
        'dzslh': '',
        'zjh': username,
        'mm': password,
        'v_yzm': code
    }
    resp = requests.post(url, headers=headers, data=form, cookies=cookies)


def get_info():
    url = 'http://222.195.242.222:8080/xjInfoAction.do?oper=xjxx'
    resp = requests.get(url, headers=headers, cookies=cookies)
    print(resp.text)


if __name__ == '__main__':
    username = input(input your username: );
    password = input(input your password: );
    get_code()
    code = input('input the code: ')
    login(username, password, code)
    get_info()

上面的代码可以拿到数据
基本思路就是
GET获取验证码时服务器会返回一个cookie
带着这个cookie再POST表单就行了
最近刚好在写学校几个网站的爬虫,遇到且解决了不少问题
这个比我们学校那个好搞……

这篇关于网页爬虫 - python requests库模拟登陆学校教务网遇到数据库繁忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆