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设计】。

相关文章

使用PyV8在Python爬虫中执行js代码

前言 可能很多人会觉得这是一个奇葩的需求,爬虫去好好的爬数据不就行了,解析js干嘛?吃饱了撑的? 搜索一下互联网上关于这个问题还真不少,但是大多数童鞋是因为自己的js基础太烂,要么是HT...

Python抓取手机号归属地信息示例代码

前言 本文给大家介绍的是利用Python抓取手机归属地信息,文中给出了详细的示例代码,相信对大家的理解和学习很有帮助,以下为Python代码,较为简单,供参考。 示例代码 # -*-...

Python使用mongodb保存爬取豆瓣电影的数据过程解析

创建爬虫项目douban scrapy startproject douban 设置items.py文件,存储要保存的数据类型和字段名称 # -*- coding: utf-8...

python Selenium爬取内容并存储至MySQL数据库的实现代码

python Selenium爬取内容并存储至MySQL数据库的实现代码

前面我通过一篇文章讲述了如何爬取CSDN的博客摘要等信息。通常,在使用Selenium爬虫爬取数据后,需要存储在TXT文本中,但是这是很难进行数据处理和数据分析的。这篇文章主要讲述通过S...

python爬虫selenium和phantomJs使用方法解析

python爬虫selenium和phantomJs使用方法解析

1.selenum:三方库。可以实现让浏览器完成自动化的操作。 2.环境搭建 2.1 安装: pip install selenium 2.2 获取浏览器的驱动程序 下载地址...