python爬虫——爬取图书馆借阅数据

环境

  • python3.6
  • BeautifulSoup4 —— v4.6

分析

    由于图书管理系统很多人密码都未改,为默认密码,刚好最近在学爬虫,想爬出来试试手,并没有任何恶意,侵删。

    本次主要包含以下内容:

  1.     模拟用户登录的程序
  2.     BeautifulSoup文档学习内容
  3.     爬取html文件的小程序

 

模拟用户登录

方法一 requests


首先利用用户名和密码,构造post数据,发送到登录页面,以形成cookie。(注意数据类型也可能是json类型)。
然后根据cookie,利用get方式请求主页。
利用requests方式如下:

import requests
import json

url = "http://interlib.sdust.edu.cn/opac/reader/space"
url_history = "http://interlib.sdust.edu.cn/opac/loan/historyLoanList"
url_log = "http://interlib.sdust.edu.cn/opac/reader/doLogin"


#获取会话
req = requests.Session()

loginid = '' #用户名
passwd = '' #密码

#构造登录请求
data = {
    'rdid' : loginid,
    'rdPasswd' : passwd,
    'returnUrl': '',
    'password': ''
}

#post模拟登录
response = req.post(url_log,data = json.dumps(data))

#get进入主页
index1 = req.get(url_history)

print(index1.status_code)
print(index1.text)


方法二 模拟登录后再携带得到的cookie访问


利用浏览器的开发者工具。转到network选项卡,并勾选Preserve Log(重要!)。

import json
import hashlib
import sys
import io
import urllib.request
import http.cookiejar

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码

headers = {'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36'}

url = "http://interlib.sdust.edu.cn/opac/reader/space"
url_history = "http://interlib.sdust.edu.cn/opac/loan/historyLoanList"
url_log = "http://interlib.sdust.edu.cn/opac/reader/doLogin"


loginid = ''
passwd = ''

md5 = hashlib.md5(passwd.encode("utf-8"))
passwd = md5.hexdigest()

data = {
    "rdid" : loginid,
    "rdPasswd" : passwd,
    'returnUrl': '/loan/historyLoanList',
    'password': ''
}
# data = json.dumps(data)

data = urllib.parse.urlencode(data).encode('utf-8')


#构造登录请求
req = urllib.request.Request(url_l
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值