python 爬取学信网登录页面的例子

yipeiwu_com5年前Python爬虫

我们以学信网为例爬取个人信息

**如果看不清楚

按照以下步骤:**

1.火狐为例 打开需要登录的网页–> F12 开发者模式 (鼠标右击,点击检查元素)–点击网络 –>需要登录的页面登录下–> 点击网络找到 一个POST提交的链接点击–>找到post(注意该post中信息就是我们提交时需要构造的表单信息)

import requests
from bs4 import BeautifulSoup
from http import cookies
import urllib
import http.cookiejar

headers = {
  'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
  'Referer':'https://account.chsi.com.cn/passport/login?service=https://my.chsi.com.cn/archive/j_spring_cas_security_check',
}

session = requests.Session()
session.headers.update(headers)
username = 'xxx'
password = 'xxx'
url = 'https://account.chsi.com.cn/passport/login?service=https://my.chsi.com.cn/archive/j_spring_cas_security_check'
def login(username,password,lt,_eventId='submit'):   #模拟登入函数
  #构造表单数据
  data = { #需要传去的数据
      '_eventId':_eventId,
      'lt':lt,
      'password':password, 
      'submit':u'登录',
      'username':username, 
  }
  html = session.post(url,data=data,headers=headers)

def get_lt(url):    #解析登入界面_eventId
  html = session.get(url)
  #获取 lt
  soup = BeautifulSoup(html.text,'lxml',from_encoding="utf-8")
  lt=soup.find('input',type="hidden")['value']
  return lt

lt = get_lt(url)#获取登录form表单信息 以学信网为例
login(username,password,lt)
login_url = 'https://my.chsi.com.cn/archive/gdjy/xj/show.action'
per_html = session.get(login_url)
soup = BeautifulSoup(per_html.text,'lxml',from_encoding="utf-8")
print(soup)
for tag in soup.find_all('table',class_='mb-table'):
  print(tag)
  for tag1 in tag.find_all('td'):
    title= tag1.get_text(); 
    print(title)

以上这篇python 爬取学信网登录页面的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现的爬虫刷回复功能示例

Python实现的爬虫刷回复功能示例

本文实例讲述了Python实现的爬虫刷回复功能。分享给大家供大家参考,具体如下: 最近闲的无聊,就想着去看看爬虫,顺着爬虫顺利的做到了模拟登录、刷帖子等等,这里简要说一下。 使用Pyth...

python爬虫获取小区经纬度以及结构化地址

本文实例为大家分享了python爬虫获取小区经纬度、地址的具体代码,供大家参考,具体内容如下 通过小区名称利用百度api可以获取小区的地址以及经纬度,但是由于api返回的值中的地址形式不...

python爬取足球直播吧五大联赛积分榜

本文实例为大家分享了python爬取足球联赛积分榜的具体代码,供大家参考,具体内容如下 使用BeautifulSoup4解析爬取足球直播吧五大联赛积分榜信息; #! /usr/bi...

编写Python爬虫抓取暴走漫画上gif图片的实例分享

本文要介绍的爬虫是抓取暴走漫画上的GIF趣图,方便离线观看。爬虫用的是python3.3开发的,主要用到了urllib、request和BeautifulSoup模块。 urllib模块...

Python实现爬取亚马逊数据并打印出Excel文件操作示例

Python实现爬取亚马逊数据并打印出Excel文件操作示例

本文实例讲述了Python实现爬取亚马逊数据并打印出Excel文件操作。分享给大家供大家参考,具体如下: python大神们别喷,代码写的很粗糙,主要是完成功能,能够借鉴就看下吧,我是学...