国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

scrapy抓取學院新聞報告實例

來源:懂視網 責編:小采 時間:2020-11-27 14:14:09
文檔

scrapy抓取學院新聞報告實例

scrapy抓取學院新聞報告實例:抓取四川大學公共管理學院官網()所有的新聞咨詢.實驗流程1.確定抓取目標.2.制定抓取規則.3.'編寫/調試'抓取規則.4.獲得抓取數據1.確定抓取目標我們這次需要抓取的目標為四川大學公共管理學院的所有新聞資訊.于是我們需要知道公管學院官網的布局結構.
推薦度:
導讀scrapy抓取學院新聞報告實例:抓取四川大學公共管理學院官網()所有的新聞咨詢.實驗流程1.確定抓取目標.2.制定抓取規則.3.'編寫/調試'抓取規則.4.獲得抓取數據1.確定抓取目標我們這次需要抓取的目標為四川大學公共管理學院的所有新聞資訊.于是我們需要知道公管學院官網的布局結構.

分別對應的知識點為:

1.爬出一個頁面下的基礎數據.
2.通過爬到的數據進行二次爬取.
3.通過循環對網頁進行所有數據的爬取.

話不多說,現在開干.

3.1爬出一頁新聞欄目下的所有新聞鏈接


Paste_Image.png

通過對新聞欄目的源代碼分析,我們發現所抓數據的結構為


Paste_Image.png

那么我們只需要將爬蟲的選擇器定位到(li:newsinfo_box_cf),再進行for循環抓取即可.

編寫代碼
import scrapyclass News2Spider(scrapy.Spider):
 name = "news_info_2"
 start_urls = ["http://ggglxy.scu.edu.cn/index.php?c=special&sid=1&page=1",
 ]def parse(self, response):for href in response.xpath("//div[@class='newsinfo_box cf']"):
 url = response.urljoin(href.xpath("div[@class='news_c fr']/h3/a/@href").extract_first())

測試,通過!


Paste_Image.png

3.2通過爬到的一頁新聞鏈接進入到新聞詳情爬取所需要數據(主要是新聞內容)

現在我獲得了一組URL,現在我需要進入到每一個URL中抓取我所需要的標題,時間和內容,代碼實現也挺簡單,只需要在原有代碼抓到一個URL時進入該URL并且抓取相應的數據即可.所以,我只需要再寫一個進入新聞詳情頁的抓取方法,并且使用scapy.request調用即可.

編寫代碼
#進入新聞詳情頁的抓取方法
def parse_dir_contents(self, response):item = GgglxyItem()item['date'] = response.xpath("//div[@class='detail_zy_title']/p/text()").extract_first()item['href'] = responseitem['title'] = response.xpath("//div[@class='detail_zy_title']/h1/text()").extract_first()
 data = response.xpath("//div[@class='detail_zy_c pb30 mb30']")item['content'] = data[0].xpath('string(.)').extract()[0]
 yield item

整合進原有代碼后,有:

import scrapyfrom ggglxy.items import GgglxyItemclass News2Spider(scrapy.Spider):
 name = "news_info_2"
 start_urls = ["http://ggglxy.scu.edu.cn/index.php?c=special&sid=1&page=1",
 ]def parse(self, response):for href in response.xpath("//div[@class='newsinfo_box cf']"):
 url = response.urljoin(href.xpath("div[@class='news_c fr']/h3/a/@href").extract_first())#調用新聞抓取方法yield scrapy.Request(url, callback=self.parse_dir_contents)#進入新聞詳情頁的抓取方法 def parse_dir_contents(self, response):
 item = GgglxyItem()
 item['date'] = response.xpath("//div[@class='detail_zy_title']/p/text()").extract_first()
 item['href'] = response
 item['title'] = response.xpath("//div[@class='detail_zy_title']/h1/text()").extract_first()
 data = response.xpath("//div[@class='detail_zy_c pb30 mb30']")
 item['content'] = data[0].xpath('string(.)').extract()[0]yield item

測試,通過!


Paste_Image.png

這時我們加一個循環:

NEXT_PAGE_NUM = 1 

NEXT_PAGE_NUM = NEXT_PAGE_NUM + 1if NEXT_PAGE_NUM<11:next_url = 'http://ggglxy.scu.edu.cn/index.php?c=special&sid=1&page=%s' % NEXT_PAGE_NUM
 yield scrapy.Request(next_url, callback=self.parse)

加入到原本代碼:

import scrapyfrom ggglxy.items import GgglxyItem

NEXT_PAGE_NUM = 1class News2Spider(scrapy.Spider):
 name = "news_info_2"
 start_urls = ["http://ggglxy.scu.edu.cn/index.php?c=special&sid=1&page=1",
 ]def parse(self, response):for href in response.xpath("//div[@class='newsinfo_box cf']"):
 URL = response.urljoin(href.xpath("div[@class='news_c fr']/h3/a/@href").extract_first())yield scrapy.Request(URL, callback=self.parse_dir_contents)global NEXT_PAGE_NUM
 NEXT_PAGE_NUM = NEXT_PAGE_NUM + 1if NEXT_PAGE_NUM<11:
 next_url = 'http://ggglxy.scu.edu.cn/index.php?c=special&sid=1&page=%s' % NEXT_PAGE_NUMyield scrapy.Request(next_url, callback=self.parse) def parse_dir_contents(self, response):
 item = GgglxyItem() 
 item['date'] = response.xpath("//div[@class='detail_zy_title']/p/text()").extract_first()
 item['href'] = response 
 item['title'] = response.xpath("//div[@class='detail_zy_title']/h1/text()").extract_first()
 data = response.xpath("//div[@class='detail_zy_c pb30 mb30']")
 item['content'] = data[0].xpath('string(.)').extract()[0] yield item

測試:


Paste_Image.png

抓到的數量為191,但是我們看官網發現有193條新聞,少了兩條.
為啥呢?我們注意到log的error有兩條:
定位問題:原來發現,學院的新聞欄目還有兩條隱藏的二級欄目:
比如:


Paste_Image.png


對應的URL為


Paste_Image.png


URL都長的不一樣,難怪抓不到了!
那么我們還得為這兩條二級欄目的URL設定專門的規則,只需要加入判斷是否為二級欄目:

 if URL.find('type') != -1: yield scrapy.Request(URL, callback=self.parse)

組裝原函數:

import scrapy
from ggglxy.items import GgglxyItem

NEXT_PAGE_NUM = 1class News2Spider(scrapy.Spider):
 name = "news_info_2"
 start_urls = ["http://ggglxy.scu.edu.cn/index.php?c=special&sid=1&page=1",
 ]def parse(self, response):for href in response.xpath("//div[@class='newsinfo_box cf']"):
 URL = response.urljoin(href.xpath("div[@class='news_c fr']/h3/a/@href").extract_first())if URL.find('type') != -1:yield scrapy.Request(URL, callback=self.parse)yield scrapy.Request(URL, callback=self.parse_dir_contents)
 global NEXT_PAGE_NUM
 NEXT_PAGE_NUM = NEXT_PAGE_NUM + 1if NEXT_PAGE_NUM<11:
 next_url = 'http://ggglxy.scu.edu.cn/index.php?c=special&sid=1&page=%s' % NEXT_PAGE_NUMyield scrapy.Request(next_url, callback=self.parse) def parse_dir_contents(self, response):
 item = GgglxyItem() 
 item['date'] = response.xpath("//div[@class='detail_zy_title']/p/text()").extract_first()
 item['href'] = response 
 item['title'] = response.xpath("//div[@class='detail_zy_title']/h1/text()").extract_first()
 data = response.xpath("//div[@class='detail_zy_c pb30 mb30']")
 item['content'] = data[0].xpath('string(.)').extract()[0] yield item

測試:


Paste_Image.png

我們發現,抓取的數據由以前的193條增加到了238條,log里面也沒有error了,說明我們的抓取規則OK!

4.獲得抓取數據

 scrapy crawl news_info_2 -o 0016.json

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

scrapy抓取學院新聞報告實例

scrapy抓取學院新聞報告實例:抓取四川大學公共管理學院官網()所有的新聞咨詢.實驗流程1.確定抓取目標.2.制定抓取規則.3.'編寫/調試'抓取規則.4.獲得抓取數據1.確定抓取目標我們這次需要抓取的目標為四川大學公共管理學院的所有新聞資訊.于是我們需要知道公管學院官網的布局結構.
推薦度:
標簽: 大學 新聞 案例
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 欧美另类日韩中文色综合 | 亚洲欧美偷拍另类 | 美国一级大黄大色毛片视频一 | 欧美色亚洲图 | 国产91精品一区二区麻豆亚洲 | 视频一区久久 | 国产精品高清一区二区三区 | 欧美激情视频一区 | 成人a毛片免费视频观看 | 久久精品免费一区二区视 | 亚洲精品综合久久中文字幕 | 欧美妞干网| 国产免费自拍 | 激情一区二区三区成人 | 伊人精品成人久久综合欧美 | 欧美日韩电影在线观看 | 另类专区另类专区亚洲 | 中文字幕久久亚洲一区 | 欧美激情视频一区二区三区 | 精品国产91久久久久 | 国产欧美视频在线观看 | 欧美激情视频一区 | 中文字幕日韩欧美 | 国内精品伊人久久久影视 | 一区二区中文字幕 | 国产日韩欧美亚洲综合 | 天天躁日日躁狠狠躁中文字幕老牛 | 在线中文高清资源免费观看 | 亚洲一级二级三级 | 亚洲国产精品日韩在线 | 亚韩在线 | 国产a久久精品一区二区三区 | a级毛片在线播放 | 亚洲精品福利在线观看 | 欧美区在线 | 麻豆精品久久久 | 日本欧美一区二区 | 久久国产精品高清一区二区三区 | 欧美日本一道本 | 日韩欧美系列 | 欧美激情一区二区三区不卡 |