B1:新聞數據采集
一、項目分析
采集對象:四川大學公共管理學院新聞動態欄
采集環境:Ubuntu+MobaXterm
采集目的:采集出四川大學公共管理學院新聞動態欄的191條新聞
二、采集步驟
1.采集新聞數據,首先要分析采集數據的網頁有什么內容,對網頁進行拆分,了解原網頁的結構布局,包含什么字段(元素)。
由上圖可知,該網頁是一個標題集+詳細頁面(list+items)的形式。
借助網頁開發者工具,查看網頁源碼,可以看見網頁對應的各個元素字段。
2.分析采集任務,明確我們要采集的內容,確定要采集的字段(元素)。
基于上一步驟的分析,我們可以看見網頁的具體形式,由此確定新聞數據采集的字段:標題(title)、時間(time)、詳情(text)、圖片(picture)。
3.制定采集方案:爬取步驟,爬取規則
爬取步驟:首先爬出標題集頁面的鏈接,一共有10頁。再通過爬出的鏈接進入詳情頁爬取每一條新聞的標題、時間、詳情以及圖片。
爬取規則:利用Xpath規則定位。
友情鏈接:Xpath語法:http://www.w3school.com.cn/xpath/xpath_syntax.asp
4.編寫代碼并調試
(1)修改item.py: 定義自己要抓取的數據。
# -- 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 NItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
time = scrapy.Field()
title = scrapy.Field()
text = scrapy.Field()
picture = scrapy.Field()
pass
tips:注意class的名稱NItem,在寫spiders中會用到。
(2)編寫spiders:
newsspider.py:
import scrapy
from news.items import NItem
class NewsSpider(scrapy.Spider):
name="newsspider"#設置爬蟲name,這是唯一定位實例的屬性
start_urls=[ #爬取的起始網頁
'http://ggglxy.scu.edu.cn/index.php?c=special&sid=1',
]
def parse(self,response):
for href in response.xpath('//div[@class="news_c fr"]/h3[@class="mb10"]/a/@href').extract():
yield scrapy.Request(response.urljoin(href),callback=self.parse_news)#將爬出的鏈接傳值給parse函數
next_page=response.xpath('//div[@class="pager cf tr pt10 pb10 mt30 mobile_dn"]/li[last()-1]/a/href').extract()
last_page=response.xpath('//div[@class="pager cf tr pt10 pb10 mt30 mobile_dn"]/li[last()]/a/href').extract()
if last_page is not None:#判斷最后一頁是否為空,如果不是,繼續爬取,如果是,則停止爬取
next_page="http://ggglxy.scu.edu.cn/"+next_page
yield scrapy.Request(next_page, callback=self.parse)
def parse_news(self,response):
item = NewsItem()
item['time'] = response.xpath('//div[@class="detail_zy_title"]/p/text()').extract()#使用xpath方式選擇信息
item['title'] = response.xpath('//div[@class="detail_zy_title"]/h1/text()').extract()
item['text'] = response.xpath('//div[@class="detail_zy_c pb30 mb30"]/p/span/text()').extract()
item['picture'] = response.xpath('//div[@class="detail_zy_c pb30 mb30"]/p/image/@src').extract()
yield item
tips:
(1)注意news.items中news一定是項目名稱。
import scrapy
from news.items import NItem#必須要寫,否則NItem沒有定義
class NewsSpider(scrapy.Spider):
沒有寫from news.items import NItem:
沒有將news.items寫成項目名稱:
(2)注意括號,符號,單詞拼寫等細節。
item['time'] = response.xpath('//div[@class="detail_zy_title"]/p/text()').extract()
item['title'] = response.xpath('//div[@class="detail_zy_title"]/h1/text()').extract()
item['text'] = response.xpath('//div[@class="detail_zy_c pb30 mb30"]/p/span/text()').extract()
item['picture'] = response.xpath('//div[@class="detail_zy_c pb30 mb30"]/p/image/@src').extract()
若將中括號[]寫成():
(3)路徑錯誤。
我之前爬錯了網頁,結果什么都沒有爬出來:
(4)注意設置判斷,結束爬取。
if last_page is not None:
next_page="http://ggglxy.scu.edu.cn/"+next_page
yield scrapy.http.Request(next_page,callback=self.parse)
如果不設置判斷,就只爬出來一部分數據:
5.提取數據
得到n.json:
若要得到中文的數據,可以修改settings.py: 添加
FEED_EXPORT_ENCODING = 'utf-8'
如下圖:
SPIDER_MODULES = ['teacher.spiders']
NEWSPIDER_MODULE = 'teacher.spiders'
FEED_EXPORT_ENCODING = 'utf-8'#中文
也可以在執行爬蟲的時候,直接輸入以下命令:
scrapy crawl yourspider -o name.json -s FEED_EXPORT_ENCODING=utf-8
得到news.json:
友情鏈接:unicode轉中文:http://www.phpstudy.net/c.php/91841.html
B2:教師資料采集
步驟和方法與B1相同。
得到相應文件:
1.item.py:
import scrapy
class TeachersItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
name = scrapy.Field()
position = scrapy.Field()
description = scrapy.Field()
2.tc.py
import scrapy
from teacher.items import TeachersItem
class TeacherSpider(scrapy.Spider):
name="tc"
start_urls=[
'http://ggglxy.scu.edu.cn/index.php?c=article&a=type&tid=18&page_1_page=1',
]
def parse(self,response):
for href in response.xpath('//div[@class="l fl"]/a/@href').extract():
yield scrapy.http.Request(response.urljoin(href), callback=self.parse_teacher)
next_page=response.xpath('//div[@class="pager cf tc pt10 pb10 mobile_dn"]/li[last()-1]/a/@href').extract_first()
last_page=response.xpath('//div[@class="pager cf tc pt10 pb10 mobile_dn"]/li[last()]/a/@href').extract_first()
if last_page is not None:
next_page="http://ggglxy.scu.edu.cn/"+next_page
yield scrapy.http.Request(next_page, callback=self.parse)
def parse_teacher(self,response):
item = TeachersItem()
item['name'] = response.xpath('//div[@class="r fr"]/h3/text()').extract()
item['position'] = response.xpath('//div[@class="r fr"]/p/text()').extract()
item['description'] = response.xpath('//div[@class="r fr"]/div/text()').extract()
yield item
3.t.json
總結
耐心+細心