利用python爬虫技术本福特_Python爬虫技术(一)--模拟登陆

好了,最后上完整代码,当当当当~

# -*- coding: utf-8 -*-

import requests

import sys

import urllib2

import re

if __name__ == "__main__":

## 这段代码是用于解决中文报错的问题

reload(sys)

sys.setdefaultencoding("utf8")

posturl = "http://ids.chd.edu.cn/authserver/login?service=http://portal.chd.edu.cn/index.portal"

#保存cookies,不保存cookie很危险,登陆成功后不保存cookie服务器将不知道你已经登录

#或者说服务器不知道你是你,就导致获得页面失败

s = requests.session()

circle = s.get(posturl).text

#查找lt字符串

#长大信息门户中有几个隐藏表单项,lt表单项为一个随机字符串

#其余几个均为固定字符串

#所以我们必须先得到lt字符串

ltString = '

ltAnswer = re.findall(ltString, circle)

lt = ltAnswer[0].replace('

#构造头部信息

head = {

'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',

'Accept-Encoding' : 'gzip,deflate',

'Accept-Language' : 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',

'Host' : 'ids.chd.edu.cn',

'Connection' : 'keep-alive',

#反爬虫技术,这个说明我们是从这个网页进入的

'Referer' : 'http://ids.chd.edu.cn/authserver/login?service=http://portal.chd.edu.cn/index.portal',

'Upgrade-Insecure-Requests' : '1',

#伪装浏览器

'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0'

}

#构造Post数据

postData = {'_eventId' : "submit",

'btn1' : "",

'dllt' : "userNamePasswordLogin",

'execution': "e1s1",

'lt' : lt,

'password' : "*******",

'rmShown' : "1",

'username' : "123456789",

}

loginhtml = s.post(posturl,data=postData,headers=head)

url2 = 'http://portal.chd.edu.cn'

head2 = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0',

'Referer' : 'http://ids.chd.edu.cn/authserver/login?service=http://portal.chd.edu.cn/index.portal'}

scorehtml = s.get(url2,headers=head2)

print scorehtml.text.decode('gbk','ignore')

好了,本次实验到此结束,欢迎一起学习更多知识!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值