一肌蜻、采集前準(zhǔn)備#
在采集之前谋旦,首先進(jìn)入四川大學(xué)公共管理學(xué)院的師資隊(duì)伍頁面查看頁面顯示結(jié)構(gòu)绊含,并用開發(fā)者工具“ctrl+shift+i”工具查看其代碼排列規(guī)則桑嘶,確定采集字段內(nèi)容。
基本思想是先在初始頁面采集姓名躬充、職稱逃顶、專業(yè)讨便、郵箱,然后通過采集的詳情頁面鏈接進(jìn)入詳情頁以政,并采集其基本簡歷霸褒,代表性研究成果,獲獎(jiǎng)情況盈蛮、科研項(xiàng)目废菱、人才培養(yǎng)。共10個(gè)字段抖誉。
過程的操作指導(dǎo)來源于scrapy的官方文檔以及老師同學(xué)的幫助殊轴。
二、新建scrapy項(xiàng)目#
首先新建一個(gè)scrapy項(xiàng)目袒炉,代碼如下:
cd venv
scrapy startproject teachersinfo
三旁理、編寫items.py文件#
然后編寫項(xiàng)目的items文件,在這個(gè)文件中定義將要采集的字段我磁。按照規(guī)定格式編寫之后孽文,這里的字段將直接用于后面步驟中的采集過程,且便于數(shù)據(jù)的傳遞十性。
代碼如下:
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html
import scrapy
class TeachersinfoItem(scrapy.Item):
# 教師名字
name = scrapy.Field()
# 教師職稱
title = scrapy.Field()
# 教師院系
major = scrapy.Field()
# 教師郵箱
mail = scrapy.Field()
# 教師簡介
resume = scrapy.Field()
# 教師簡介
achieve = scrapy.Field()
# 教師代表性研究成果
prize = scrapy.Field()
# 教師獲獎(jiǎng)情況
project = scrapy.Field()
# 教師科研項(xiàng)目
training = scrapy.Field()
# 教師人才培養(yǎng)
pass
四叛溢、編寫爬蟲#
在項(xiàng)目之下塑悼,可以看到基本結(jié)構(gòu):
在spiders之下新建一個(gè)爬蟲劲适,命名為:teachers_spider.py
import scrapy
import hashlib
from scrapy.selector import Selector
from teachersinfo.items import TeachersinfoItem
class TeachersinfoSpider(scrapy.Spider):
name = "teachersinfo"
start_urls = [
'http://ggglxy.scu.edu.cn/index.php?c=article&a=type&tid=18&page_1_page=1',
]
def parse(self, response):
for info in response.xpath("http://ul[@class='teachers_ul mt20 cf']/li"):
item = TeachersinfoItem()
item['name'] = info.xpath("div[@class='r fr']/h3/text()").extract_first()
item['title'] = info.xpath("div[@class='r fr']/p/text()").extract_first()
item['major'] = info.xpath("div[@class='r fr']/div[@class='desc']/p[1]/text()").extract_first().split("E-mail:")[-1]
item['mail'] = info.xpath("div[@class='r fr']/div[@class='desc']/p[2]/text()").extract_first()
href = info.xpath("div[@class='l fl']/a/@href").extract_first()
yield scrapy.Request(response.urljoin(href), meta={'item': item}, callback=self.parse_more_info)
next_page = response.xpath("http://div[@class='pager cf tc pt10 pb10 mobile_dn']/li[last()-1]/a/@href").extract_first()
last_page = response.xpath("http://div[@class='pager cf tc pt10 pb10 mobile_dn']/li[last()]/a/@href").extract_first()
if last_page:
next_page = "http://ggglxy.scu.edu.cn/"+next_page
yield scrapy.http.Request(next_page, callback=self.parse)
@staticmethod
def parse_more_info(response):
item = response.meta['item']
item['resume'] = response.xpath("http://div[@class='desc']/text()").extract()
item['achieve'] = "".join(response.xpath('/html/body/div[3]/div[2]/div/div[2]//text()').extract())
item['prize'] = "".join(response.xpath('/html/body/div[3]/div[2]/div/div[3]//text()').extract())
item['project'] = "".join(response.xpath('/html/body/div[3]/div[2]/div/div[4]//text()').extract())
item['training'] = "".join(response.xpath('/html/body/div[3]/div[2]/div/div[5]//text()').extract())
yield item
通過其中meta方法的傳遞參數(shù),將基礎(chǔ)頁面的內(nèi)容與詳情頁面的內(nèi)容聯(lián)系起來厢蒜,不用重復(fù)采集兩次教師的姓名以及職稱霞势。
五、執(zhí)行爬蟲斑鸦,并保存文件#
直接用一句代碼實(shí)現(xiàn)這兩步過程愕贡,將結(jié)果保存為csv格式。
scrapy crawl teachersinfo -o infos.csv
其結(jié)果顯示如下:
將infos.csv文件下載并打開巷屿,發(fā)現(xiàn)excel內(nèi)容為亂碼固以,于是采用記事本打開并另存,將編碼方式換成ANSI嘱巾,然后再用excel打開即可呈現(xiàn)結(jié)果憨琳。顯示如下:
六、補(bǔ)充#
關(guān)于翻頁###
在進(jìn)行分頁時(shí)旬昭,嘗試使用其他方式篙螟,其中xpath的sibling是一個(gè)很好的方式。
將翻頁代碼更改一下:
next_page = response.xpath('/html/body/div[4]/div[2]/div[1]/div[2]/li[@class]/following-sibling::*[1]//@href').extract_first()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.http.Request(next_page, callback=self.parse)
保存成csv文件问拘,用相同轉(zhuǎn)編碼的方式查看遍略,可知可以得到相同的結(jié)果惧所。