python實(shí)現(xiàn)Scrapy爬取網(wǎng)易新聞
1. 新建項(xiàng)目
在命令行窗口下輸入scrapy startproject scrapytest
, 如下
然后就自動(dòng)創(chuàng)建了相應(yīng)的文件,如下
2. 修改itmes.py文件
打開scrapy框架自動(dòng)創(chuàng)建的items.py文件,如下
# Define here the models for your scraped items # # See documentation in: # https://docs.scrapy.org/en/latest/topics/items.html import scrapy class ScrapytestItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() pass
編寫里面的代碼,確定我要獲取的信息,比如新聞標(biāo)題,url,時(shí)間,來源,來源的url,新聞的內(nèi)容等
class ScrapytestItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() title = scrapy.Field() timestamp = scrapy.Field() category = scrapy.Field() content = scrapy.Field() url = scrapy.Field() pass
3. 定義spider,創(chuàng)建一個(gè)爬蟲模板
3.1 創(chuàng)建crawl爬蟲模板
在命令行窗口下面 創(chuàng)建一個(gè)crawl爬蟲模板(注意在文件的根目錄下面,指令檢查別輸入錯(cuò)誤,-t 表示使用后面的crawl模板),會(huì)在spider文件夾生成一個(gè)news163.py文件
scrapy genspider -t crawl codingce news.163.com
然后看一下這個(gè)‘crawl'模板和一般的模板有什么區(qū)別,多了鏈接提取器還有一些爬蟲規(guī)則,這樣就有利于我們做一些深度信息的爬取
import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule class CodingceSpider(CrawlSpider): name = 'codingce' allowed_domains = ['163.com'] start_urls = ['http://news.163.com/'] rules = ( Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True), ) def parse_item(self, response): item = {} #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get() #item['name'] = response.xpath('//div[@id="name"]').get() #item['description'] = response.xpath('//div[@id="description"]').get() return item
3.2 補(bǔ)充知識(shí):selectors選擇器
支持xpath和css,xpath語法如下
/html/head/title /html/head/title/text() //td (深度提取的話就是兩個(gè)/) //div[@class=‘mine']
3.3. 分析網(wǎng)頁內(nèi)容
在谷歌chrome瀏覽器下,打在網(wǎng)頁新聞的網(wǎng)站,選擇查看源代碼,確認(rèn)我們可以獲取到itmes.py文件的內(nèi)容(其實(shí)那里面的要獲取的就是查看了網(wǎng)頁源代碼之后確定可以獲取的)
確認(rèn)標(biāo)題、時(shí)間、url、來源url和內(nèi)容可以通過檢查和標(biāo)簽對(duì)應(yīng)上,比如正文部分
主體
標(biāo)題
時(shí)間
分類
4. 修改spider下創(chuàng)建的爬蟲文件
4.1 導(dǎo)入包
打開創(chuàng)建的爬蟲模板,進(jìn)行代碼的編寫,除了導(dǎo)入系統(tǒng)自動(dòng)創(chuàng)建的三個(gè)庫(kù),我們還需要導(dǎo)入news.items(這里就涉及到了包的概念了,最開始說的–init–.py文件存在說明這個(gè)文件夾就是一個(gè)包可以直接導(dǎo)入,不需要安裝)
注意:使用的類ExampleSpider一定要繼承自CrawlSpider,因?yàn)樽铋_始我們創(chuàng)建的就是一個(gè)‘crawl'的爬蟲模板,對(duì)應(yīng)上
import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule from scrapytest.items import ScrapytestItem class CodingceSpider(CrawlSpider): name = 'codingce' allowed_domains = ['163.com'] start_urls = ['http://news.163.com/'] rules = ( Rule(LinkExtractor(allow=r'.*\.163\.com/\d{2}/\d{4}/\d{2}/.*\.html'), callback='parse', follow=True), ) def parse(self, response): item = {} content = '<br>'.join(response.css('.post_content p::text').getall()) if len(content) < 100: return return item
Rule(LinkExtractor(allow=r'..163.com/\d{2}/\d{4}/\d{2}/..html'), callback=‘parse', follow=True), 其中第一個(gè)allow里面是書寫正則表達(dá)式的(也是我們核心要輸入的內(nèi)容),第二個(gè)是回調(diào)函數(shù),第三個(gè)表示是否允許深入
最終代碼
from datetime import datetime import re import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule from scrapytest.items import ScrapytestItem class CodingceSpider(CrawlSpider): name = 'codingce' allowed_domains = ['163.com'] start_urls = ['http://news.163.com/'] rules = ( Rule(LinkExtractor(allow=r'.*\.163\.com/\d{2}/\d{4}/\d{2}/.*\.html'), callback='parse', follow=True), ) def parse(self, response): item = {} content = '<br>'.join(response.css('.post_content p::text').getall()) if len(content) < 100: return title = response.css('h1::text').get() category = response.css('.post_crumb a::text').getall()[-1] print(category, "=======category") time_text = response.css('.post_info::text').get() timestamp_text = re.search(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', time_text).group() timestamp = datetime.fromisoformat(timestamp_text) print(title, "=========title") print(content, "===============content") print(timestamp, "==============timestamp") print(response.url) return item
到此這篇關(guān)于python實(shí)現(xiàn)Scrapy爬取網(wǎng)易新聞的文章就介紹到這了,更多相關(guān)python Scrapy爬取網(wǎng)易新聞內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python爬蟲之教你利用Scrapy爬取圖片
- python基于scrapy爬取京東筆記本電腦數(shù)據(jù)并進(jìn)行簡(jiǎn)單處理和分析
- Scrapy+Selenium自動(dòng)獲取cookie爬取網(wǎng)易云音樂個(gè)人喜愛歌單
- 如何在scrapy中集成selenium爬取網(wǎng)頁的方法
- 使用scrapy ImagesPipeline爬取圖片資源的示例代碼
- scrapy與selenium結(jié)合爬取數(shù)據(jù)(爬取動(dòng)態(tài)網(wǎng)站)的示例代碼
- Python利用Scrapy框架爬取豆瓣電影示例
- Python scrapy增量爬取實(shí)例及實(shí)現(xiàn)過程解析
- Python爬蟲實(shí)戰(zhàn)之使用Scrapy爬取豆瓣圖片
相關(guān)文章
Python實(shí)現(xiàn)的堆排序算法原理與用法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)的堆排序算法,簡(jiǎn)單描述了堆排序的原理,并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)堆排序的相關(guān)操作技巧,代碼中備有較為詳細(xì)的注釋便于理解,需要的朋友可以參考下2017-11-11django之導(dǎo)入并執(zhí)行自定義的函數(shù)模塊圖解
這篇文章主要介紹了django之導(dǎo)入并執(zhí)行自定義的函數(shù)模塊圖解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04淺談Tensorflow 動(dòng)態(tài)雙向RNN的輸出問題
今天小編就為大家分享一篇淺談Tensorflow 動(dòng)態(tài)雙向RNN的輸出問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01python使用tensorflow深度學(xué)習(xí)識(shí)別驗(yàn)證碼
這篇文章主要介紹了python使用tensorflow深度學(xué)習(xí)識(shí)別驗(yàn)證碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Python實(shí)現(xiàn)Pig Latin小游戲?qū)嵗a
這篇文章主要介紹了Python實(shí)現(xiàn)Pig Latin小游戲?qū)嵗a,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02Python使用sftp實(shí)現(xiàn)傳文件夾和文件
這篇文章主要為大家詳細(xì)介紹了Python使用sftp實(shí)現(xiàn)傳文件夾和文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04