本次信息采集的起始網(wǎng)址為:http://ggglxy.scu.edu.cn/index.php?c=article&a=type&tid=18, 并利用scrapy進(jìn)行采集
采集過程:(起始)
由于之前已經(jīng)嘗試了采集數(shù)據(jù)赵誓,相關(guān)包的安裝過程以及虛擬環(huán)境的搭建就不再一一贅述了们何。
1彪腔、創(chuàng)建采集項(xiàng)目
切換到venv目錄下巷蚪,執(zhí)行命令scrapy startproject teacher居灯,則可創(chuàng)建teacher的項(xiàng)目
2穗熬、編寫和執(zhí)行爬蟲镀迂,并保存為xml
本次采集涉及到翻頁和獲取詳情頁內(nèi)容兩個方面,因此本次爬蟲的編寫也分兩個部分來寫唤蔗,最后進(jìn)行整合探遵。首先是獲取詳情頁的內(nèi)容窟赏,再是翻頁。
2.1 獲取詳情內(nèi)容
執(zhí)行結(jié)果如下:
保存為xml:
在這個過程中遇到的問題就是沒辦法把詳情頁的內(nèi)容和對應(yīng)的人物信息關(guān)聯(lián)起來箱季,無法出現(xiàn)在一個共同的標(biāo)簽下面涯穷,嘗試過將獲取詳情頁的網(wǎng)址放在上面yield的大括號里面,結(jié)果出現(xiàn)報(bào)錯藏雏。
2.2翻頁
執(zhí)行結(jié)果如下:
保存為xml:
因?yàn)榉撚泻芏鄒rl拷况,目前還沒有學(xué)會如何識別這些網(wǎng)址的方法。本次抓取的思路是利用循環(huán)的方式掘殴,因此是想要采集下一頁的url赚瘦,而在翻頁的網(wǎng)址列表中不僅有多個網(wǎng)址,而且每個頁面都至少有兩個相同的網(wǎng)址奏寨,那就是下一頁與前面具體的某一頁起意。在觀察<html>代碼時發(fā)現(xiàn),下一頁的url所在的
<li>
標(biāo)簽的style的width屬性是與其他不同的病瞳,因此考慮到將這個限制條件加到爬蟲的代碼中揽咕。本以為可以抓到所有的數(shù)據(jù),但是當(dāng)循環(huán)到第3頁的時候套菜,網(wǎng)頁中出現(xiàn)了上一頁亲善,而這個的width屬性是與下一頁的相同,因此由直接進(jìn)入了第一頁笼踩,循環(huán)結(jié)束逗爹。因此本次采集僅僅采集到了24條數(shù)據(jù),其中第一頁的8條數(shù)據(jù)出現(xiàn)了兩次嚎于。
2.3整合
3修改及完善
經(jīng)歷前幾次的嘗試以及后面的學(xué)習(xí)掘而,對爬蟲進(jìn)行修改,獲取比較完整的信息于购。
3.1對象分析
本次采集內(nèi)容列表頁共16頁袍睡,每頁8位老師,每位老師對應(yīng)有一個詳情頁肋僧。列表頁需采集5個字段包括姓名(name)斑胜、職稱(position)、系別(department)嫌吠、聯(lián)系方式(email)止潘、詳情頁鏈接(link),詳情頁采集5個字段辫诅,包括老師簡介(intro)凭戴、研究成果(output)、獲獎情況(award)炕矮、科研項(xiàng)目(project)么夫、人才培養(yǎng)(training)
元數(shù)據(jù)字段 | html | xpath |
---|---|---|
name | <h3 class="mb10">董歡</h3> | //li[@class="fl"]/div[@class="r fr"]/h3/text() |
position | <p class="color_main f14">講師</p> | //li[@class="fl"]/ div[@class="r fr"]/p/text() |
department | <p> 土地資源與房地產(chǎn)管理系 </p> | //li[@class="fl"]/div[@class="r fr"]/div[@class="desc"]/p[1]/text() |
<p> E-mail:dhuan8844@126.com </p> | //li[@class="fl"]/div[@class="r fr"]/div[@class="desc"]/p[2]/text() | |
link | <a href="index.php?c=article&id=7" target="_blank">[圖片上傳失敗...(image-9c5868-1534753409166)]</a> | //li[@class="fl"]/div[@class="l fl"]/a/@href |
intro | <div class="desc">管理學(xué)博士...... </div> | //div[@class="r fr"]/div[@class="desc"]/text() |
output | <div class="detailbox mt20" style="">...</div> | //div[@class="detailbox mt20"][1]//text() |
award | <div class="detailbox mt20" style="">...</div> | //div[@class="detailbox mt20"][2]//text() |
project | <div class="detailbox mt20" style="">...</div> | //div[@class="detailbox mt20"][3]//text() |
training | <div class="detailbox mt20" style="">...</div> | //div[@class="detailbox mt20"][4]//text() |
3.2采集策略
利用xpath者冤,首先采集列表頁基本信息的5個字段,通過詳情頁鏈接進(jìn)入詳情頁進(jìn)行具體內(nèi)容(包括5個字段)的采集档痪,其中通過meta方法進(jìn)行數(shù)據(jù)的傳遞涉枫。通過分析網(wǎng)頁<html>結(jié)構(gòu),翻頁則通過頁碼鏈接列表中當(dāng)前頁的兄弟節(jié)點(diǎn)的下一個節(jié)點(diǎn)來找到下一頁的鏈接腐螟, 再通過response.urljoin()方法形成絕對鏈接愿汰。
3.3scrapy項(xiàng)目
創(chuàng)建虛擬環(huán)境、采集項(xiàng)目等過程這里就不詳細(xì)說明了乐纸。
3.3.1定義items.py
# -*- coding: utf-8 -*-
import scrapy
class LaoshiItem(scrapy.Item):
# define the fields for your item here like:
name = scrapy.Field()
position = scrapy.Field()
department = scrapy.Field()
email = scrapy.Field()
link = scrapy.Field()
intro = scrapy.Field()
output = scrapy.Field()
award = scrapy.Field()
project = scrapy.Field()
training = scrapy.Field()
3.3.2編寫抓取老師信息爬蟲lsinfo.py
import scrapy
from laoshi.items import LaoshiItem
class TeacherSpider(scrapy.Spider):
name = "lsinfo"
start_urls = [
'http://ggglxy.scu.edu.cn/index.php?c=article&a=type&tid=18',
]
def parse(self, response):
for teacher in response.xpath('//li[@class="fl"]'):
item = LaoshiItem()
name = teacher.xpath('div[@class="r fr"]/h3/text()').extract_first()
position = teacher.xpath('div[@class="r fr"]/p/text()').extract_first()
department = teacher.xpath('div[@class="r fr"]/div[@class="desc"]/p[1]/text()').extract_first()
email = teacher.xpath('div[@class="r fr"]/div[@class="desc"]/p[2]/text()').extract_first()
link = teacher.xpath('div[@class="l fl"]/a/@href').extract_first()
item['name'] = name
item['position'] = position
item['department'] = department
item['email'] = email
yield scrapy.Request(response.urljoin(link), callback=self.parse_detail, meta={ 'item': item })
next_page = response.xpath('//div[@class="pager cf tc pt10 pb10 mobile_dn"]/li[@class="c"]/following-sibling::*[1]/a/@href').extract_first()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.Request(next_page, callback=self.parse)
def parse_detail(self, response):
item = response.meta['item']
item['intro'] = response.xpath('//div[@class="r fr"]/div[@class="desc"]/text()').extract()
item['output'] = "".join(response.xpath('//div[@class="detailbox mt20"][1]//text()').extract())
item['award'] = "".join(response.xpath('//div[@class="detailbox mt20"][2]//text()').extract())
item['project'] = "".join(response.xpath('//div[@class="detailbox mt20"][3]//text()').extract())
item['training'] = "".join(response.xpath('//div[@class="detailbox mt20"][4]//text()').extract())
yield item
其中尼桶,翻頁使用的是:/li[@class="c"]/following-sibling::[1],因?yàn)榫W(wǎng)頁結(jié)構(gòu)為下一頁的url在當(dāng)前頁節(jié)點(diǎn)的下一個節(jié)點(diǎn)锯仪。
列表信息與詳情頁信息對接泵督,采用的方法是在scrapy.Request中使用meta參數(shù)傳遞,即meta={ 'item': item },在回滾的函數(shù)中使用item = response.meta['item']*
note:需要注意的是庶喜,獲取詳情頁的url字段應(yīng)該與其他字段在一個循環(huán)中小腊,以避免抓取重復(fù)數(shù)據(jù)
3.3抓取結(jié)果