• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答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
    主站蜘蛛池模板: 国产精品免费观看视频| 在线精品无码字幕无码AV| 日本精品自产拍在线观看中文| 凹凸国产熟女精品视频app| 无码精品人妻一区二区三区影院 | 99re热视频这里只精品| 亚洲欧美日韩久久精品| 精品久久综合1区2区3区激情| 亚洲国产精品一区| 成人国产精品一区二区网站| 亚洲日韩国产AV无码无码精品| 亚洲AⅤ永久无码精品AA| 国产一成人精品福利网站| 久久精品国产亚洲av日韩| 亚洲精品无码专区在线播放| 九九久久精品无码专区| 999久久久无码国产精品 | 国产三级精品三级在线专区1| 永久无码精品三区在线4| 久久精品无码一区二区三区日韩| 国产精品内射视频免费| 四虎精品成人免费永久| 国产精品久久免费| 国产成人精品AA毛片| 麻豆成人久久精品二区三区免费 | 国产人成精品午夜在线观看| 99精品一区二区三区无码吞精| 久久99精品久久久久久hb无码| 婷婷精品国产亚洲AV麻豆不片| 亚洲精品国产综合久久一线| 午夜精品久久久内射近拍高清| 久久久无码精品亚洲日韩软件| 国产午夜亚洲精品国产成人小说| 国产精品户外野外| 国产精品最新国产精品第十页| 国产精品手机在线观看你懂的| 国产精品 综合 第五页| 99热这里只有精品6国产免费| 四虎永久在线精品884aa下载| 四虎成人欧美精品在永久在线| 国产精品第一页在线|