四川大學公共管理學院教師信息采集(起始及修改)

本次信息采集的起始網址為:http://ggglxy.scu.edu.cn/index.php?c=article&a=type&tid=18, 并利用scrapy進行采集

采集過程:(起始)
由于之前已經嘗試了采集數(shù)據,相關包的安裝過程以及虛擬環(huán)境的搭建就不再一一贅述了。
1、創(chuàng)建采集項目
切換到venv目錄下,執(zhí)行命令scrapy startproject teacher,則可創(chuàng)建teacher的項目

創(chuàng)建采集項目.png

2、編寫和執(zhí)行爬蟲,并保存為xml
本次采集涉及到翻頁和獲取詳情頁內容兩個方面,因此本次爬蟲的編寫也分兩個部分來寫,最后進行整合。首先是獲取詳情頁的內容,再是翻頁。
2.1 獲取詳情內容

詳情頁代碼.png

執(zhí)行結果如下:

抓取詳情(1).png

抓取詳情(2).png

保存為xml:
詳情xml.png

在這個過程中遇到的問題就是沒辦法把詳情頁的內容和對應的人物信息關聯(lián)起來,無法出現(xiàn)在一個共同的標簽下面,嘗試過將獲取詳情頁的網址放在上面yield的大括號里面,結果出現(xiàn)報錯。
2.2翻頁

分頁代碼.png

執(zhí)行結果如下:

抓取分頁(1).png
抓取分頁(2).png

保存為xml:

翻頁xml.png

因為翻頁有很多url,目前還沒有學會如何識別這些網址的方法。本次抓取的思路是利用循環(huán)的方式,因此是想要采集下一頁的url,而在翻頁的網址列表中不僅有多個網址,而且每個頁面都至少有兩個相同的網址,那就是下一頁與前面具體的某一頁。在觀察<html>代碼時發(fā)現(xiàn),下一頁的url所在的<li>標簽的style的width屬性是與其他不同的,因此考慮到將這個限制條件加到爬蟲的代碼中。本以為可以抓到所有的數(shù)據,但是當循環(huán)到第3頁的時候,網頁中出現(xiàn)了上一頁,而這個的width屬性是與下一頁的相同,因此由直接進入了第一頁,循環(huán)結束。因此本次采集僅僅采集到了24條數(shù)據,其中第一頁的8條數(shù)據出現(xiàn)了兩次。

頁碼.png

2.3整合

整合代碼.png
分頁與詳情(1).png
分頁與詳情(2).png
整合xml.png

3修改及完善
經歷前幾次的嘗試以及后面的學習,對爬蟲進行修改,獲取比較完整的信息。
3.1對象分析
本次采集內容列表頁共16頁,每頁8位老師,每位老師對應有一個詳情頁。列表頁需采集5個字段包括姓名(name)、職稱(position)、系別(department)、聯(lián)系方式(email)、詳情頁鏈接(link),詳情頁采集5個字段,包括老師簡介(intro)、研究成果(output)、獲獎情況(award)、科研項目(project)、人才培養(yǎng)(training)

元數(shù)據字段 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> 土地資源與房地產管理系 </p> //li[@class="fl"]/div[@class="r fr"]/div[@class="desc"]/p[1]/text()
email <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">管理學博士...... </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個字段,通過詳情頁鏈接進入詳情頁進行具體內容(包括5個字段)的采集,其中通過meta方法進行數(shù)據的傳遞。通過分析網頁<html>結構,翻頁則通過頁碼鏈接列表中當前頁的兄弟節(jié)點的下一個節(jié)點來找到下一頁的鏈接, 再通過response.urljoin()方法形成絕對鏈接。

3.3scrapy項目
創(chuàng)建虛擬環(huán)境、采集項目等過程這里就不詳細說明了。

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],因為網頁結構為下一頁的url在當前頁節(jié)點的下一個節(jié)點。
列表信息與詳情頁信息對接,采用的方法是在scrapy.Request中使用meta參數(shù)傳遞,即
meta={ 'item': item },在回滾的函數(shù)中使用item = response.meta['item']*
note:需要注意的是,獲取詳情頁的url字段應該與其他字段在一個循環(huán)中,以避免抓取重復數(shù)據

3.3抓取結果

抓取結果.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容