python&MongoDB爬取图书馆借阅记录


Posted in Python onFebruary 05, 2016

直接上需求和代码
首先是需要爬取的链接和网页:http://211.81.31.34/uhtbin/cgisirsi/x/0/0/57/49?user_id=LIBSCI_ENGI&password=LIBSC

python&MongoDB爬取图书馆借阅记录

登陆进去之后进入我的账号——借阅、预约及申请记录——借阅历史就可以看到所要爬取的内容

python&MongoDB爬取图书馆借阅记录

然后将借阅历史中的题名、著者、借阅日期、归还日期、索书号存入Mongodb数据库中,以上便是这次爬虫的需求。

下面开始:

各软件版本为:

  • python 2.7.11
  • MongoDb 3.2.1
  • Pycharm 5.0.4
  • MongoDb Management Studio 1.9.3
  • 360极速浏览器 懒得查了

一、登陆模块
python中的登陆一般都是用urllib和urllib2这两个模块,首先我们要查看网页的源代码:
<form name="loginform" method="post" action="/uhtbin/cgisirsi/?ps=nPdFje4RP9/理工大学馆/125620449/303">
<!--  Copyright (c) 2004, Sirsi Corporation - myProfile login or view myFavorites -->
<!--  Copyright (c) 1998 - 2003, Sirsi Corporation - Sets the default values for USER_ID, ALT_ID, and PIN prompts. - The USER_ID, ALT_ID, and PIN page variables will be returned. -->

<!-- If the user has not logged in, first try to default to the ID based on the IP address - the $UO and $Uf will be set.  If that fails, then default to the IDs in the config file. If the user has already logged in, default to the logged in user's IDs, unless the user is a shared login. -->

 

      <!-- only user ID is used if both on -->
        <div class="user_name">
            <label for="user_id">借阅证号码:</label>
            <input class="user_name_input" type="text" name="user_id" id="user_id"  maxlength="20" value=""/>
        </div>
   
    
        <div class="password">
            <label for="password">个人密码:</label>
            <input class="password_input" type="password" name="password" id="password"  maxlength="20" value=""/>
        </div> 
   
    
    <input type="submit" value="用户登录" class="login_button"/>
查找网页中的form表单中的action,方法为post,但是随后我们发现,该网页中的action地址不是一定的,是随机变化的,刷新一下就变成了下面这样子的:
 <form name="loginform" method="post" action="/uhtbin/cgisirsi/?ps=1Nimt5K1Lt/理工大学馆/202330426/303">
我们可以看到/?ps到/之间的字符串是随机变化的(加粗部分),于是我们需要用到另一个模块——BeautifulSoup实时获取该链接:
url = "http://211.81.31.34/uhtbin/cgisirsi/x/0/0/57/49?user_id=LIBSCI_ENGI&password=LIBSC"
   res = urllib2.urlopen(url).read()
    soup = BeautifulSoup(res, "html.parser")
login_url = "http://211.81.31.34" + soup.findAll("form")[1]['action'].encode("utf8")
之后就可以正常使用urllib和urllib来模拟登陆了,下面列举一下BeautifulSoup的常用方法,之后的HTML解析需要:

1.soup.contents 该属性可以将tag的子节点以列表的方式输出

2.soup.children 通过tag的.children生成器,可以对tag的子节点进行循环

3.soup.parent 获取某个元素的父节点

4.soup.find_all(name,attrs,recursive,text,**kwargs) 搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件

5.soup.find_all("a",class="xx") 按CSS搜索

6.find(name,attrs,recursive,text,**kwargs) 可以通过limit和find_all区分开

二、解析所获得的HTML

先看看需求中的HTML的特点:
<tbody id="tblSuspensions">
<!-- OCLN changed Listcode to Le to support charge history -->
     <!-- SIRSI_List Listcode="LN" -->

     <tr>
       <td class="accountstyle" align="left">
          <!-- SIRSI_Conditional IF List_DC_Exists="IB" AND NOT List_DC_Comp="IB^" -->
<!-- Start title here -->
 <!-- Title -->
            做人要低调,说话要幽默 孙郡铠编著
        </td>
       <td class="accountstyle author" align="left">
          <!-- Author -->
            孙郡铠 编著
        </td>
       <td class="accountstyle due_date" align="center">
          <!-- Date Charged -->
            2015/9/10,16:16
       </td>
        <td class="accountstyle due_date" align="left">
        <!-- Date Returned -->
            2015/9/23,15:15
        </td>

        <td class="accountstyle author" align="center">
          <!-- Call Number -->
            B821-49/S65
        </td>

      </tr>

     <tr>
       <td class="accountstyle" align="left">
          <!-- SIRSI_Conditional IF List_DC_Exists="IB" AND NOT List_DC_Comp="IB^" -->
<!-- Start title here -->
 <!-- Title -->
            我用一生去寻找 潘石屹的人生哲学 潘石屹著
        </td>
       <td class="accountstyle author" align="left">
          <!-- Author -->
            潘石屹, 1963- 著
        </td>
       <td class="accountstyle due_date" align="center">
          <!-- Date Charged -->
            2015/9/10,16:16
       </td>
        <td class="accountstyle due_date" align="left">
        <!-- Date Returned -->
            2015/9/25,15:23
        </td>

        <td class="accountstyle author" align="center">
          <!-- Call Number -->
            B821-49/P89
        </td>

      </tr>
由所有代码,注意这行:

<tbody id="tblSuspensions">
该标签表示下面的内容将是借阅书籍的相关信息,我们采用遍历该网页所有子节点的方法获得id="tblSuspensions"的内容:
   for i, k in enumerate(BeautifulSoup(detail, "html.parser").find(id='tblSuspensions').children):
     # print i,k
        if isinstance(k, element.Tag):
             bookhtml.append(k)
                # print type(k)
三、提取所需要的内容

这一步比较简单,bs4中的BeautifulSoup可以轻易的提取:
for i in bookhtml:
                # p
                # rint i
                name = i.find(class_="accountstyle").getText()
                author = i.find(class_="accountstyle author", align="left").getText()
                Date_Charged = i.find(class_="accountstyle due_date", align="center").getText()
                Date_Returned = i.find(class_="accountstyle due_date", align="left").getText()
                bookid = i.find(class_="accountstyle author", align="center").getText()
                bookinfo.append(
                    [name.strip(), author.strip(), Date_Charged.strip(), Date_Returned.strip(), bookid.strip()])
这一步采用getText()的方法将text中内容提取出来;strip()方法是去掉前后空格,同时可以保留之间的空格,比如:s="   a a  ",使用s.strip()之后即为"a a"

四、连接数据库
据说NoSQL以后会很流行,随后采用了Mongodb数据库图图新鲜,结果一折腾真是烦,具体安装方法在上一篇日记中记载了。
1.导入python连接Mongodb的模块

import pymongo
2.创建python和Mongodb的链接:
# connection database
conn = pymongo.MongoClient("mongodb://root:root@localhost:27017")
db = conn.book
collection = db.book
3.将获得的内容保存到数据库:
user = {"_id": xuehao_ben,
                        "Bookname": name.strip(),
                        "Author": author.strip(),
                        "Rent_Day": Date_Charged.strip(),
                        "Return_Day": Date_Returned.strip()}
                j += 1
                collection.insert(user)
上面基本完成了,但是爬虫做到这个没有意义,重点在下面

五、获取全校学生的借阅记录

我们学校的图书馆的密码都是一样的,应该没有人闲得无聊改密码,甚至没有人用过这个网站去查询自己的借阅记录,所以,做个循环,就可以轻易的获取到全校的借阅记录了,然后并没有那么简单,str(0001)强制将int变成string,但是在cmd的python中是报错的(在1位置),在pycharm前面三个0是忽略的,只能用傻瓜式的四个for循环了。好了,下面是所有代码:
# encoding=utf8
import urllib2
import urllib
import pymongo
import socket

from bs4 import BeautifulSoup
from bs4 import element

# connection database
conn = pymongo.MongoClient("mongodb://root:root@localhost:27017")
db = conn.book
collection = db.book

# 循环开始
def xunhuan(xuehao):
    try:
        socket.setdefaulttimeout(60)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(("127.0.0.1", 80))
        url = "http://211.81.31.34/uhtbin/cgisirsi/x/0/0/57/49?user_id=LIBSCI_ENGI&password=LIBSC"
        res = urllib2.urlopen(url).read()
        soup = BeautifulSoup(res, "html.parser")
        login_url = "http://211.81.31.34" + soup.findAll("form")[1]['action'].encode("utf8")
        params = {
            "user_id": "账号前缀你猜你猜" + xuehao,
            "password": "密码你猜猜"
        }
        print params
        params = urllib.urlencode(params)
        req = urllib2.Request(login_url, params)
        lianjie = urllib2.urlopen(req)
        # print lianjie
        jieyue_res = lianjie.read()
        # print jieyue_res     首页的HTML代码
        houmian = BeautifulSoup(jieyue_res, "html.parser").find_all('a', class_='rootbar')[1]['href']
        # print houmian
        houmian = urllib.quote(houmian.encode('utf8'))
        url_myaccount = "http://211.81.31.34" + houmian
        # print url_myaccount
        # print urllib.urlencode(BeautifulSoup(jieyue_res, "html.parser").find_all('a',class_ = 'rootbar')[0]['href'])

        lianjie2 = urllib.urlopen(url_myaccount)
        myaccounthtml = lianjie2.read()
        detail_url = ''
        # print (BeautifulSoup(myaccounthtml).find_all('ul',class_='gatelist_table')[0]).children
        print "连接完成,开始爬取数据"
        for i in (BeautifulSoup(myaccounthtml, "html.parser").find_all('ul', class_='gatelist_table')[0]).children:
            if isinstance(i, element.NavigableString):
                continue
            for ii in i.children:
                detail_url = ii['href']
            break
        detail_url = "http://211.81.31.34" + urllib.quote(detail_url.encode('utf8'))
        detail = urllib.urlopen(detail_url).read()
        # print detail
        bookhtml = []
        bookinfo = []

        # 解决没有借书
        try:
            for i, k in enumerate(BeautifulSoup(detail, "html.parser").find(id='tblSuspensions').children):
                # print i,k
                if isinstance(k, element.Tag):
                    bookhtml.append(k)
                    # print type(k)
            print "look here!!!"
            j = 1
            for i in bookhtml:
                # p
                # rint i
                name = i.find(class_="accountstyle").getText()
                author = i.find(class_="accountstyle author", align="left").getText()
                Date_Charged = i.find(class_="accountstyle due_date", align="center").getText()
                Date_Returned = i.find(class_="accountstyle due_date", align="left").getText()
                bookid = i.find(class_="accountstyle author", align="center").getText()
                bookinfo.append(
                    [name.strip(), author.strip(), Date_Charged.strip(), Date_Returned.strip(), bookid.strip()])
                xuehao_ben = str(xuehao) + str("_") + str(j)
                user = {"_id": xuehao_ben,
                        "Bookname": name.strip(),
                        "Author": author.strip(),
                        "Rent_Day": Date_Charged.strip(),
                        "Return_Day": Date_Returned.strip()}
                j += 1
                collection.insert(user)
        except Exception, ee:
            print ee
            print "此人没有借过书"
            user = {"_id": xuehao,
                    "Bookname": "此人",
                    "Author": "没有",
                    "Rent_Day": "借过",
                    "Return_Day": "书"}
            collection.insert(user)

        print "********" + str(xuehao) + "_Finish"+"**********"
    except Exception, e:
        s.close()
        print e
        print "socket超时,重新运行"
        xunhuan(xuehao)

# with contextlib.closing(urllib.urlopen(req)) as A:
#    print A
#   print xuehao
# print req

for i1 in range(0, 6):
    for i2 in range(0, 9):
        for i3 in range(0, 9):
            for i4 in range(0, 9):
                xueha = str(i1) + str(i2) + str(i3) + str(i4)
                chushi = '0000'
                if chushi == xueha:
                    print "=======爬虫开始=========="
                else:
                    print xueha + "begin"
                    xunhuan(xueha)

conn.close()
print "End!!!"
下面是Mongodb Management Studio的显示内容(部分):

 python&amp;MongoDB爬取图书馆借阅记录

 

总结:这次爬虫遇到了很多问题,问了很多人,但是最终效果还不是很理想,虽然用了try except语句,但是还是会报错10060,连接超时(我只能质疑学校的服务器了TT),还有就是,你可以看到数据库中列的顺序不一样=。=这个我暂时未理解,希望大家可以给出解决方法。

以上就是本文的全部内容,希望对大家的学习有所帮助。

Python 相关文章推荐
Python Mysql数据库操作 Perl操作Mysql数据库
Jan 12 Python
python实现的udp协议Server和Client代码实例
Jun 04 Python
python通过floor函数舍弃小数位的方法
Mar 17 Python
flask框架视图函数用法示例
Jul 19 Python
对Tensorflow中的变量初始化函数详解
Jul 27 Python
django 数据库连接模块解析及简单长连接改造方法
Aug 29 Python
Python lambda表达式filter、map、reduce函数用法解析
Sep 11 Python
Python实现手机号自动判断男女性别(实例解析)
Dec 22 Python
深入了解NumPy 高级索引
Jul 24 Python
解决pytorch 模型复制的一些问题
Mar 03 Python
python爬不同图片分别保存在不同文件夹中的实现
Apr 02 Python
python生成随机数、随机字符、随机字符串
Apr 06 Python
Python中常用操作字符串的函数与方法总结
Feb 04 #Python
Python中的字符串类型基本知识学习教程
Feb 04 #Python
Python的math模块中的常用数学函数整理
Feb 04 #Python
详解Python编程中基本的数学计算使用
Feb 04 #Python
Python连接mysql数据库的正确姿势
Feb 03 #Python
Python内建数据结构详解
Feb 03 #Python
Python解析树及树的遍历
Feb 03 #Python
You might like
在mysql数据库原有字段后增加新内容
2009/11/26 PHP
php xml 入门学习资料
2011/01/01 PHP
PHP CURL 多线程操作代码实例
2015/05/13 PHP
深入浅析用PHP实现MVC
2016/03/02 PHP
PHP操作FTP类 (上传、下载、移动、创建等)
2016/03/31 PHP
PHP快速排序quicksort实例详解
2016/09/28 PHP
fromCharCode和charCodeAt 方法
2006/12/27 Javascript
jQuery 联动日历实现代码
2012/05/31 Javascript
Ubuntu中搭建Nodejs开发环境过程分享
2014/06/01 NodeJs
JavaScript事件委托用法分析
2015/01/24 Javascript
简介JavaScript中的italics()方法的使用
2015/06/08 Javascript
JavaScript中的splice方法用法详解
2016/07/20 Javascript
bootstrap轮播模板使用方法详解
2017/11/17 Javascript
微信小程序wx.request实现后台数据交互功能分析
2017/11/25 Javascript
浅谈Node.js 中间件模式
2018/06/12 Javascript
解决前后端分离 vue+springboot 跨域 session+cookie失效问题
2019/05/13 Javascript
JavaScript如何借用构造函数继承
2019/11/06 Javascript
vue props对象validator自定义函数实例
2019/11/13 Javascript
JavaScript多种滤镜算法实现代码实例
2019/12/10 Javascript
js实现九宫格布局效果
2020/05/28 Javascript
详解vue中v-on事件监听指令的基本用法
2020/07/22 Javascript
一份python入门应该看的学习资料
2018/04/11 Python
pytorch + visdom 处理简单分类问题的示例
2018/06/04 Python
Python3自动生成MySQL数据字典的markdown文本的实现
2020/05/07 Python
英国最大的电子产品和家电零售企业:Currys PC World
2016/09/24 全球购物
美国大尺码女装零售商:TORRID
2016/10/01 全球购物
学生拾金不昧表扬信
2014/01/21 职场文书
《彭德怀和他的大黑骡子》教学反思
2014/04/12 职场文书
外贸会计专业自荐信
2014/06/22 职场文书
中学学校门卫岗位职责
2014/08/15 职场文书
试用期辞职信范文
2015/03/02 职场文书
2015年幼儿教师个人工作总结
2015/05/20 职场文书
爱的教育读书笔记
2015/06/26 职场文书
2019奶茶店创业计划书范本!
2019/07/15 职场文书
windows安装python超详细图文教程
2021/05/21 Python
安装Ruby和 Rails的详细步骤
2022/04/19 Ruby