一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python&MongoDB爬取圖書館借閱記錄

python&MongoDB爬取圖書館借閱記錄

2020-08-11 17:37w1570631036 Python

這篇文章主要介紹了python&MongoDB爬取圖書館借閱記錄的相關(guān)資料,需要的朋友可以參考下

直接上需求和代碼
首先是需要爬取的鏈接和網(wǎng)頁:http://211.81.31.34/uhtbin/cgisirsi/x/0/0/57/49?user_id=LIBSCI_ENGI&password=LIBSC

python&MongoDB爬取圖書館借閱記錄

登陸進(jìn)去之后進(jìn)入我的賬號(hào)——借閱、預(yù)約及申請(qǐng)記錄——借閱歷史就可以看到所要爬取的內(nèi)容

python&MongoDB爬取圖書館借閱記錄

然后將借閱歷史中的題名、著者、借閱日期、歸還日期、索書號(hào)存入Mongodb數(shù)據(jù)庫中,以上便是這次爬蟲的需求。

下面開始:

各軟件版本為:

  • python 2.7.11
  • MongoDb 3.2.1
  • Pycharm 5.0.4
  • MongoDb Management Studio 1.9.3
  • 360極速瀏覽器 懶得查了

一、登陸模塊
python中的登陸一般都是用urllib和urllib2這兩個(gè)模塊,首先我們要查看網(wǎng)頁的源代碼:

  1. <form name="loginform" method="post" action="/uhtbin/cgisirsi/?ps=nPdFje4RP9/理工大學(xué)館/125620449/303"
  2. <!--  Copyright (c) 2004, Sirsi Corporation - myProfile login or view myFavorites --> 
  3. <!--  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. --> 
  4.  
  5. <!-- 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 indefault to the logged in user's IDs, unless the user is a shared login. --> 
  6.  
  7.  
  8.  
  9.       <!-- only user ID is used if both on --> 
  10.         <div class="user_name"
  11.             <label for="user_id">借閱證號(hào)碼:</label> 
  12.             <input class="user_name_input" type="text" name="user_id" id="user_id"  maxlength="20" value=""/> 
  13.         </div> 
  14.     
  15.      
  16.         <div class="password"
  17.             <label for="password">個(gè)人密碼:</label> 
  18.             <input class="password_input" type="password" name="password" id="password"  maxlength="20" value=""/> 
  19.         </div>  
  20.     
  21.      
  22.     <input type="submit" value="用戶登錄" class="login_button"/> 

查找網(wǎng)頁中的form表單中的action,方法為post,但是隨后我們發(fā)現(xiàn),該網(wǎng)頁中的action地址不是一定的,是隨機(jī)變化的,刷新一下就變成了下面這樣子的:

  1. <form name="loginform" method="post" action="/uhtbin/cgisirsi/?ps=1Nimt5K1Lt/理工大學(xué)館/202330426/303"


我們可以看到/?ps到/之間的字符串是隨機(jī)變化的(加粗部分),于是我們需要用到另一個(gè)模塊——BeautifulSoup實(shí)時(shí)獲取該鏈接:

  1. url = "http://211.81.31.34/uhtbin/cgisirsi/x/0/0/57/49?user_id=LIBSCI_ENGI&password=LIBSC" 
  2.    res = urllib2.urlopen(url).read() 
  3.     soup = BeautifulSoup(res, "html.parser"
  4. login_url = "http://211.81.31.34" + soup.findAll("form")[1]['action'].encode("utf8"


之后就可以正常使用urllib和urllib來模擬登陸了,下面列舉一下BeautifulSoup的常用方法,之后的HTML解析需要:

1.soup.contents 該屬性可以將tag的子節(jié)點(diǎn)以列表的方式輸出

2.soup.children 通過tag的.children生成器,可以對(duì)tag的子節(jié)點(diǎn)進(jìn)行循環(huán)

3.soup.parent 獲取某個(gè)元素的父節(jié)點(diǎn)

4.soup.find_all(name,attrs,recursive,text,**kwargs) 搜索當(dāng)前tag的所有tag子節(jié)點(diǎn),并判斷是否符合過濾器的條件

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

6.find(name,attrs,recursive,text,**kwargs) 可以通過limit和find_all區(qū)分開

二、解析所獲得的HTML

先看看需求中的HTML的特點(diǎn):

  1. <tbody id="tblSuspensions"
  2. <!-- OCLN changed Listcode to Le to support charge history --> 
  3.      <!-- SIRSI_List Listcode="LN" --> 
  4.  
  5.      <tr> 
  6.        <td class="accountstyle" align="left"
  7.           <!-- SIRSI_Conditional IF List_DC_Exists="IB" AND NOT List_DC_Comp="IB^" --> 
  8. <!-- Start title here --> 
  9.  <!-- Title --> 
  10.             做人要低調(diào),說話要幽默 孫郡鎧編著 
  11.         </td> 
  12.        <td class="accountstyle author" align="left"
  13.           <!-- Author --> 
  14.             孫郡鎧 編著 
  15.         </td> 
  16.        <td class="accountstyle due_date" align="center"
  17.           <!-- Date Charged --> 
  18.             2015/9/10,16:16 
  19.        </td> 
  20.         <td class="accountstyle due_date" align="left"
  21.         <!-- Date Returned --> 
  22.             2015/9/23,15:15 
  23.         </td> 
  24.  
  25.         <td class="accountstyle author" align="center"
  26.           <!-- Call Number --> 
  27.             B821-49/S65 
  28.         </td> 
  29.  
  30.       </tr> 
  31.  
  32.      <tr> 
  33.        <td class="accountstyle" align="left"
  34.           <!-- SIRSI_Conditional IF List_DC_Exists="IB" AND NOT List_DC_Comp="IB^" --> 
  35. <!-- Start title here --> 
  36.  <!-- Title --> 
  37.             我用一生去尋找 潘石屹的人生哲學(xué) 潘石屹著 
  38.         </td> 
  39.        <td class="accountstyle author" align="left"
  40.           <!-- Author --> 
  41.             潘石屹, 1963- 著 
  42.         </td> 
  43.        <td class="accountstyle due_date" align="center"
  44.           <!-- Date Charged --> 
  45.             2015/9/10,16:16 
  46.        </td> 
  47.         <td class="accountstyle due_date" align="left"
  48.         <!-- Date Returned --> 
  49.             2015/9/25,15:23 
  50.         </td> 
  51.  
  52.         <td class="accountstyle author" align="center"
  53.           <!-- Call Number --> 
  54.             B821-49/P89 
  55.         </td> 
  56.  
  57.       </tr> 

由所有代碼,注意這行:

  1. <tbody id="tblSuspensions"


該標(biāo)簽表示下面的內(nèi)容將是借閱書籍的相關(guān)信息,我們采用遍歷該網(wǎng)頁所有子節(jié)點(diǎn)的方法獲得id="tblSuspensions"的內(nèi)容:

  1. for i, k in enumerate(BeautifulSoup(detail, "html.parser").find(id='tblSuspensions').children): 
  2.   # print i,k 
  3.      if isinstance(k, element.Tag): 
  4.           bookhtml.append(k) 
  5.              # print type(k) 


三、提取所需要的內(nèi)容

這一步比較簡單,bs4中的BeautifulSoup可以輕易的提取:
for i in bookhtml:

  1. # p 
  2. # rint i 
  3. name = i.find(class_="accountstyle").getText() 
  4. author = i.find(class_="accountstyle author", align="left").getText() 
  5. Date_Charged = i.find(class_="accountstyle due_date", align="center").getText() 
  6. Date_Returned = i.find(class_="accountstyle due_date", align="left").getText() 
  7. bookid = i.find(class_="accountstyle author", align="center").getText() 
  8. bookinfo.append( 
  9.     [name.strip(), author.strip(), Date_Charged.strip(), Date_Returned.strip(), bookid.strip()]) 


這一步采用getText()的方法將text中內(nèi)容提取出來;strip()方法是去掉前后空格,同時(shí)可以保留之間的空格,比如:s="   a a  ",使用s.strip()之后即為"a a"

四、連接數(shù)據(jù)庫
據(jù)說NoSQL以后會(huì)很流行,隨后采用了Mongodb數(shù)據(jù)庫圖圖新鮮,結(jié)果一折騰真是煩,具體安裝方法在上一篇日記中記載了。
1.導(dǎo)入python連接Mongodb的模塊
  import pymongo
2.創(chuàng)建python和Mongodb的鏈接:
# connection database
conn = pymongo.MongoClient("mongodb://root:root@localhost:27017")
db = conn.book
collection = db.book
3.將獲得的內(nèi)容保存到數(shù)據(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)
上面基本完成了,但是爬蟲做到這個(gè)沒有意義,重點(diǎn)在下面

五、獲取全校學(xué)生的借閱記錄

  我們學(xué)校的圖書館的密碼都是一樣的,應(yīng)該沒有人閑得無聊改密碼,甚至沒有人用過這個(gè)網(wǎng)站去查詢自己的借閱記錄,所以,做個(gè)循環(huán),就可以輕易的獲取到全校的借閱記錄了,然后并沒有那么簡單,str(0001)強(qiáng)制將int變成string,但是在cmd的python中是報(bào)錯(cuò)的(在1位置),在pycharm前面三個(gè)0是忽略的,只能用傻瓜式的四個(gè)for循環(huán)了。好了,下面是所有代碼:

  1. # encoding=utf8 
  2. import urllib2 
  3. import urllib 
  4. import pymongo 
  5. import socket 
  6.  
  7. from bs4 import BeautifulSoup 
  8. from bs4 import element 
  9.  
  10. # connection database 
  11. conn = pymongo.MongoClient("mongodb://root:root@localhost:27017"
  12. db = conn.book 
  13. collection = db.book 
  14.  
  15.  
  16. # 循環(huán)開始 
  17. def xunhuan(xuehao): 
  18.     try
  19.         socket.setdefaulttimeout(60) 
  20.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
  21.         s.bind(("127.0.0.1", 80)) 
  22.         url = "http://211.81.31.34/uhtbin/cgisirsi/x/0/0/57/49?user_id=LIBSCI_ENGI&password=LIBSC" 
  23.         res = urllib2.urlopen(url).read() 
  24.         soup = BeautifulSoup(res, "html.parser"
  25.         login_url = "http://211.81.31.34" + soup.findAll("form")[1]['action'].encode("utf8"
  26.         params = { 
  27.             "user_id""賬號(hào)前綴你猜你猜" + xuehao, 
  28.             "password""密碼你猜猜" 
  29.         } 
  30.         print params 
  31.         params = urllib.urlencode(params) 
  32.         req = urllib2.Request(login_url, params) 
  33.         lianjie = urllib2.urlopen(req) 
  34.         # print lianjie 
  35.         jieyue_res = lianjie.read() 
  36.         # print jieyue_res     首頁的HTML代碼 
  37.         houmian = BeautifulSoup(jieyue_res, "html.parser").find_all('a', class_='rootbar')[1]['href'
  38.         # print houmian 
  39.         houmian = urllib.quote(houmian.encode('utf8')) 
  40.         url_myaccount = "http://211.81.31.34" + houmian 
  41.         # print url_myaccount 
  42.         # print urllib.urlencode(BeautifulSoup(jieyue_res, "html.parser").find_all('a',class_ = 'rootbar')[0]['href']) 
  43.  
  44.         lianjie2 = urllib.urlopen(url_myaccount) 
  45.         myaccounthtml = lianjie2.read() 
  46.         detail_url = '' 
  47.         # print (BeautifulSoup(myaccounthtml).find_all('ul',class_='gatelist_table')[0]).children 
  48.         print "連接完成,開始爬取數(shù)據(jù)" 
  49.         for i in (BeautifulSoup(myaccounthtml, "html.parser").find_all('ul', class_='gatelist_table')[0]).children: 
  50.             if isinstance(i, element.NavigableString): 
  51.                 continue 
  52.             for ii in i.children: 
  53.                 detail_url = ii['href'
  54.             break 
  55.         detail_url = "http://211.81.31.34" + urllib.quote(detail_url.encode('utf8')) 
  56.         detail = urllib.urlopen(detail_url).read() 
  57.         # print detail 
  58.         bookhtml = [] 
  59.         bookinfo = [] 
  60.  
  61.         # 解決沒有借書 
  62.         try
  63.             for i, k in enumerate(BeautifulSoup(detail, "html.parser").find(id='tblSuspensions').children): 
  64.                 # print i,k 
  65.                 if isinstance(k, element.Tag): 
  66.                     bookhtml.append(k) 
  67.                     # print type(k) 
  68.             print "look here!!!" 
  69.             j = 1 
  70.             for i in bookhtml: 
  71.                 # p 
  72.                 # rint i 
  73.                 name = i.find(class_="accountstyle").getText() 
  74.                 author = i.find(class_="accountstyle author", align="left").getText() 
  75.                 Date_Charged = i.find(class_="accountstyle due_date", align="center").getText() 
  76.                 Date_Returned = i.find(class_="accountstyle due_date", align="left").getText() 
  77.                 bookid = i.find(class_="accountstyle author", align="center").getText() 
  78.                 bookinfo.append( 
  79.                     [name.strip(), author.strip(), Date_Charged.strip(), Date_Returned.strip(), bookid.strip()]) 
  80.                 xuehao_ben = str(xuehao) + str("_") + str(j) 
  81.                 user = {"_id": xuehao_ben, 
  82.                         "Bookname": name.strip(), 
  83.                         "Author": author.strip(), 
  84.                         "Rent_Day": Date_Charged.strip(), 
  85.                         "Return_Day": Date_Returned.strip()} 
  86.                 j += 1 
  87.                 collection.insert(user) 
  88.         except Exception, ee: 
  89.             print ee 
  90.             print "此人沒有借過書" 
  91.             user = {"_id": xuehao, 
  92.                     "Bookname""此人"
  93.                     "Author""沒有"
  94.                     "Rent_Day""借過"
  95.                     "Return_Day""書"
  96.             collection.insert(user) 
  97.  
  98.         print "********" + str(xuehao) + "_Finish"+"**********" 
  99.     except Exception, e: 
  100.         s.close() 
  101.         print e 
  102.         print "socket超時(shí),重新運(yùn)行" 
  103.         xunhuan(xuehao) 
  104.  
  105.  
  106. # with contextlib.closing(urllib.urlopen(req)) as A: 
  107. #    print A 
  108. #   print xuehao 
  109. # print req 
  110.  
  111. for i1 in range(0, 6): 
  112.     for i2 in range(0, 9): 
  113.         for i3 in range(0, 9): 
  114.             for i4 in range(0, 9): 
  115.                 xueha = str(i1) + str(i2) + str(i3) + str(i4) 
  116.                 chushi = '0000' 
  117.                 if chushi == xueha: 
  118.                     print "=======爬蟲開始==========" 
  119.                 else
  120.                     print xueha + "begin" 
  121.                     xunhuan(xueha) 
  122.  
  123. conn.close() 
  124. print "End!!!" 

下面是Mongodb Management Studio的顯示內(nèi)容(部分):

python&MongoDB爬取圖書館借閱記錄

   總結(jié):這次爬蟲遇到了很多問題,問了很多人,但是最終效果還不是很理想,雖然用了try except語句,但是還是會(huì)報(bào)錯(cuò)10060,連接超時(shí)(我只能質(zhì)疑學(xué)校的服務(wù)器了TT),還有就是,你可以看到數(shù)據(jù)庫中列的順序不一樣=。=這個(gè)我暫時(shí)未理解,希望大家可以給出解決方法。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 爱爱亚洲| 99热国产这里只有精品99 | 亚洲一区二区三区在线播放 | 四虎影院精品在线观看 | 国产成人+亚洲欧洲 | 四缺一写的小说 | 亚洲伦理天堂 | 偷偷狠狠的日日高清完整视频 | 办公室恋情在线 | 亚洲欧美一区二区三区在线观看 | 欧美一级特黄aaa大片 | 精品国语国产在线对白 | 美国videos| 好爽轻点太大了太深了 | 国产在线99 | www.最色| 女教师巨大乳孔中文字幕免费 | 卫生间被教官做好爽HH视频 | 日出水了特别黄的视频 | 奇米影视在线视频 | 性做久久久久久久久老女人 | 思敏1一5集国语版免费观看 | 娇妻被朋友征服中文字幕 | 特黄特黄一级高清免费大片 | 天堂在线国产 | 爸爸的宝贝小说全文在线阅读 | 久久er99热精品一区二区 | 交欧美 | 日本搜子同屋的日子2国语 日本爽p大片免费观看 | 日本人欧美xx | 国产精品不卡高清在线观看 | 国产精品露脸国语对白手机视频 | 91天堂一区二区 | 欧美日韩一区二区三区在线播放 | 精品国产区 | 欧美国产影院 | 99久久香蕉 | 成年人视频免费在线观看 | 日本免费高清在线观看播放 | 欧美高清videosex极品 | 美女奶口隐私免费视频网站 |