Hey
这是我的第一篇博客。
本次实例完整程序地址:源码地址
这是一篇完全给新手写的爬虫教程、也是我第一次写博文···也不知道怎么写…
我们所要做的是随时获取燕大图书馆内书籍的馆藏信息,并保存到本地,就本着给完全不懂的新手弄明白为目的来讲解吧
基础环境部分:
工欲其事必先利器,要想随时获得燕大图书馆的书籍信息,我们需要下面这几把斧子!
-
Python基础运行环境: 本篇教程采用Python3 来写
-
Requests: urllib的升级版本打包了全部功能并简化了使用方法
-
beautifulsoup: 一个可以从HTML或XML文件中提取数据的
-
LXML: 一个HTML解析包 用于辅助beautifulsoup解析网页
-
PyCharm: 一个颜值特高又好用的PythonIDE工具!
爬虫简介及原理:
网络爬虫,也叫网络蜘蛛(Web Spider)。它根据网页地址(URL)爬取网页内容,而网页地址(URL)就是我们在浏览器中输入的网站链接。比如:
http://baidu.com 它就是一个URL。
一个简单爬虫的诞生大慨需要下面几个步骤
- 爬虫入口:顾名思义我需要程序从什么地方开始获取网页
- 存储数据:如果获取的网页有你需要的内容则取出数据保存
- 找到资料所在的地址:如果你获取到的网页没有你需要的数 据,但是有前往该数据页面的地址URL,则获取这个地址URL,再获取该URL的页面内容(也就等于当作爬虫入口了)
在讲解爬虫内容之前,我们需要先学习一项写爬虫的必备技能:审查元素(如果已掌握,可跳过此部分内容)。
- 审查元素
在浏览器的地址栏输入URL地址,在网页处右键单击,找到检查。(不同浏览器的叫法不同,Chrome浏览器叫做检查,Firefox浏览器叫做查看元素,但是功能都是相同的)
进入燕大图书馆官网http://library.ysu.edu.cn/index.aspx
- 简单实例
网络爬虫的第一步就是根据URL,获取网页的HTML信息。在Python3中,可以使用urllib.request和requests进行网页爬取。
-
urllib库是python内置的,无需我们额外安装,只要安装了Python就可以使用这个库。
-
requests库是第三方库,需要我们自己安装。
requests库强大好用,所以本文使用requests库获取网页的HTML信息。
requests库的github地址:https://github.com/requests/requests
爬虫实战
- 实战背景
燕大图书馆: http://library.ysu.edu.cn/index.aspx
本文以《鲁迅全集》为例获取相应图书信息
- 操作步骤
- 我们的第一段代码:用作获取鲁迅全集这个页面。
import requests ##导入requests
from bs4 import BeautifulSoup ##导入bs4中的BeautifulSoup
import os
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}##浏览器请求头(大部分网站没有这个请求头会报错、请务必加上)
all_url = 'http://202.206.242.99:8080/opac/openlink.php?strSearchType=title&match_flag=forward&historyCount=1&strText=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&displaypg=20&showmode=list&sort=CATA_DATE&orderby=desc&location=ALL' ##开始的URL地址
start_html = requests.get(all_url, headers=headers) ##使用requests中的get方法来获取all_url(就是:http://www.mzitu.com/all这个地址)的内容 headers为上面设置的请求头、请务必参考requests官方文档解释
print(start_html.text) ##打印出start_html (请注意,concent是二进制的数据,一般用于下载图片、视频、音频、等多媒体内容是才使用concent, 对于打印网页内容请使用text)
可以看到浏览器刷新后的网页:
以下是返回来的html文本:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta name="referrer" content="no-referrer" />
<!-- 主菜单 -->
<title>燕山大学图书馆v5.6书目检索系统 </title>
<link type="text/css" rel="stylesheet" href="../tpl/css/style.css">
<link type="text/css" rel="stylesheet" href="../tpl/css/mylib.css" />
<link type="text/css" rel="stylesheet" href="../tpl/css/ui-lightness/jquery-ui.css">
<link type="text/css" rel="stylesheet" href="../tpl/css/font-awesome.css">
<script type="text/javascript" src="../tpl/js/jquery.js"></script>
<script type="text/javascript" src="../tpl/js/jquery-ui.js"></script>
<script type="text/javascript" src="../tpl/js/highlighter.js"></script>
<script type="text/javascript" src="../tpl/js/md5.js"></script>
<script type="text/javascript" src="../tpl/js/base64.js"></script>
</head>
<body>
<div id="header_opac">
<h1 class="header_opac_img">
<div class="header_opac_logo">
燕山大学图书馆书目检索系统<br>
<p>Online Public Access Catalogue</p>
</div>
</h1>
<div class="header_right header_right_top">
<p class="header_right_font"> </p>
<p style="margin-top: 60px;" class="header_right_font">
<select class="option" name="locale" size="1" style="width: 60px;"
onchange="changeLocale(this.value);">
<option value='zh_CN' selected>中文</option><option value='en_US'>English</option> </select>
| <a href="../opac/book_cart.php">暂存书架(<span id="bookcartCount">0</span>)
</a> | <font color="blue"></font> <a href="../reader/login.php" style="color:#ff0000;">登录</a></p>
</div>
</div>
<div id="menubar">
<ul id="menu_top">
<li><a class="menu_top_on" style="color: #6C5B23;"
href="../opac/search_adv.php" target="_self">书目检索</a></li>
<li><a href="../top/top_lend.php" target="_self">热门推荐</a></li>
<li><a href="../browse/cls_browsing.php" target="_self">分类浏览</a></li>
<li><a href="../newbook/newbook_cls_browse.php" target="_self">新书通报</a></li>
<li><a href="../peri/peri_nav_e.php" target="_self">期刊导航</a></li>
<li><a href="../asord/asord_hist.php" target="_self">读者荐购</a></li>
<li><a href="../shelf/curriculum_browse.php" target="_self">学科参考</a></li>
<li><a href="../info/info_guide.php" target="_self">信息发布</a></li>
<li><a href="../reader/redr_info.php" target="_self">我的图书馆</a></li>
<div class="clear"></div>
</ul>
</div>
<div id="submenu">
<a href="../opac/search_adv.php" target="_self">馆藏检索</a>
<a href="../opac/search.php" target="_self">简单检索</a>
<a href="../opac/search_more.php" target="_self">多字段检索</a>
<div class="clear"></div>
</div>
<script type="text/javascript">
function changeLocale(obj) {
$.ajax({
url: "../locale/ajax_change_locale.php",
data: "lan="+obj,
type: 'POST',
success: function(html){
// setTimeout(function(){ //使用 setTimeout()方法设定定时2000毫秒
// window.location.reload(true);//页面刷新
// },500);
window.location.reload(true);
}
});
}
</script>
<style type="text/css">
.prompt {
position: relative;
border: 1px dot #EDD28B;
background: #FFFDEE;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15);
border-radius: 1px;
padding: 9px;
}
.tooltip s {
margin-left: 2px;
border: 4px dashed;
border-color: #005A9D transparent transparent;
border-top-style: solid;
}
.tooltip s, .tooltip i {
display: inline-block;
line-height: 0;
font-size: 0;
height: 0;
overflow: hidden;
vertical-align: middle;
}
</style>
<div id="mainbox">
<div id="container">
<div id="sidebar">
<div class="left_content left_content_line">
<h4>参考翻译</h4>
<p><span id="dict"></span></p>
</div>
<div class="left_content left_content_line">
<h4>缩小检索范围</h4>
<dl class="s_l_list">
<dd><b>分类:</b></dd>
<dd>·<a href="?location=ALL&title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&hist_cont=hist&callno=I">文学</a>(77)</dd>
</dl>
<dl class="s_l_list">
<dt><strong>文献类型: </strong></dt>
<dd>·<a href="?location=ALL&title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&hist_cont=hist&doctype=01">中文图书
</a>(75)</dd>
</dl>
<dl class="s_l_list">
<dt><strong>馆藏地: </strong></dt>
<dd>·<a href="?title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&location=&location=&location=&location=00013">社科书库2(407)</a>(42)</dd>
<dd>·<a href="?title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&location=&location=&location=&location=jy2 ">教师阅览室2</a>(35)</dd>
<dd>·<a href="?title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&location=&location=&location=&location=L03 ">里仁社科2</a>(18)</dd>
<dd>·<a href="?title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&location=&location=&location=&location=00010">社科书库3(501)</a>(16)</dd>
<dd>·<a href="?title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&location=&location=&location=&location=WFXY ">文法学院资料室</a>(15)</dd>
<dd>·<a href="?title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&location=&location=&location=&location=mjsk ">社科密集书库</a>(2)</dd>
</dl>
<dl class="s_l_list">
<dt><strong>主题:</strong></dt>
<dd>·<a href="?location=ALL&title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&hist_cont=hist&subject=%E9%B2%81%E8%BF%85%E8%91%97%E4%BD%9C">鲁迅著作</a>(75)</dd>
<dd>·<a href="?location=ALL&title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no&with_ebook=off&hist_cont=hist&subject=%E5%85%A8%E9%9B%86">全集</a>(31)</dd>
</dl>
</div>
</div>
<div id="content">
<div class="book_article">
<div class="search_form bulk-actions" >
<p style="font-size:14px;">检索到 <strong class="red">75</strong> 条 题名=<font color="red">鲁
迅全集</font> </font> 的结果
<a target="_blank" href="search_rss.php?location=ALL&title=%E9%B2%81%E8%BF%85%E5%85%A8%E9%9B%86&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=yes&with_ebook=off&with_cadal=off"><img src="../tpl/images/rss.gif" title="此检索条件下的结果RSS源" border="0" /></a>
</p>
</div>
<div class="search_form bulk-actions" >
<form action="openlink.php" method="get" id="f" >
<p><select name="s2_type" class="option" size="1">
<option value="title">题 名</option>
<option value="author">责任者</option>
<option value="keyword">主题词</option>
<option value="isbn">ISBN/ISSN</option>
<option value="asordno">订购号</option>
<option value="coden">分类号</option>
<option value="callno">索书号</option>
<option value="publisher">出版社</option>
<option value="series">丛书名</option>
<option value="tpinyin">题名拼音</option>
<option value="apinyin">责任者拼音</option>
</select> <input type="text" maxlength="250" name="s2_text" id="s2_text" value="" style="width:330px;"/>
<input type="button" class="btn btn-primary" onclick="my_submit('result');" value="在结果中检索
" /> <input class="btn btn-primary" type="button" onclick="my_submit('new');" value="重新检索" />
<input type="hidden" id="search_bar" name="search_bar" value="result" />
<input name="title" type="hidden" value="鲁迅全集" />
<input type="hidden" name="doctype" value="ALL" />
<input type="hidden" name="with_ebook" value="0" />
<input type="hidden" name="match_flag" value="forward" />
<input type="hidden" name="showmode" value="list" />
<input type="hidden" name="location" value="ALL" />
</p>
<!-- CSRF TOKEN -->
<input type="hidden" id="csrf_token" name="csrf_token" value="$~hO{Kox7+" />
<!-- END -->
</form></div>
</div>
<div class="book_article">
<br/>
</div>
<div class="book_article">
<p class="booktab_line">
<span class="booktab_current"><a href="#">所有图书</a></span>
网页便由html语言编写,其中使用的图片,视频甚至音频等多媒体语言都由html中"调用",文本语言直接写入html,各种超文本语言由超链接实现嵌入html语言中。如下:
<img src="http://202.206.242.99:8080/tpl/images/star0.gif" title="此检索条件下的结果RSS源" border="0" />
我们的任务是获取相关搜索的所有书名、作者、馆藏数目以及图书状况。我们先观察一下整个网页的结构,由web中图书的顺序排列,我们猜想每本书的相关信息是否也整齐直观的的排列呢?
事实上网站制作者也真的整齐排列了每本书的信息段落,可以看到每本书对应一个标签<li>......</li>
。那么总体思路便是我们弄清一本书的相关信息(书全名,借阅状态以及馆藏地点),剩下的循环来搞定嘛。
定位到书籍全名与作者
同理得到馆藏地与借阅信息,不过鉴于每本书的馆藏信息不同,为了考虑到各种情况,我们应该多看几本书的标签。