Python scrapy爬取起點中文網(wǎng)小說榜單
爬取排行榜小說的作者,書名,分類以及完結或連載
二、項目分析目標url:“https://www.qidian.com/rank/hotsales?style=1&page=1”
通過控制臺搜索發(fā)現(xiàn)相應信息均存在于html靜態(tài)網(wǎng)頁中,所以此次爬蟲難度較低。
通過控制臺觀察發(fā)現(xiàn),需要的內(nèi)容都在一個個li列表中,每一個列表代表一本書的內(nèi)容。
在li中找到所需的內(nèi)容
找到第兩頁的url“https://www.qidian.com/rank/hotsales?style=1&page=1”“https://www.qidian.com/rank/hotsales?style=1&page=2”對比找到頁數(shù)變化開始編寫scrapy程序。
三、程序編寫創(chuàng)建項目太簡單,不說了
1.編寫item(數(shù)據(jù)存儲)
import scrapyclass QidianHotItem(scrapy.Item): name = scrapy.Field() #名稱 author = scrapy.Field() #作者 type = scrapy.Field() #類型 form= scrapy.Field() #是否完載
2.編寫spider(數(shù)據(jù)抓取(核心代碼))
#coding:utf-8from scrapy import Requestfrom scrapy.spiders import Spiderfrom ..items import QidianHotItem#導入下需要的庫class HotSalesSpider(Spider):#設置spider的類 name = 'hot' #爬蟲的名稱 qidian_header={'user-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'} #設置header current_page = 1 #爬蟲起始頁 def start_requests(self): #重寫第一次請求url='https://www.qidian.com/rank/hotsales?style=1&page=1'yield Request(url,headers=self.qidian_header,callback=self.hot_parse)#Request發(fā)起鏈接請求#url:目標url#header:設置頭部(模擬瀏覽器)#callback:設置頁面抓起方式(空默認為parse) def hot_parse(self, response):#數(shù)據(jù)解析#xpath定位list_selector=response.xpath('//div[@class=’book-mid-info’]')#獲取所有小說for one_selector in list_selector: #獲取小說信息 name=one_selector.xpath('h4/a/text()').extract()[0] #獲取作者 author=one_selector.xpath('p[1]/a[1]/text()').extract()[0] #獲取類型 type=one_selector.xpath('p[1]/a[2]/text()').extract()[0] # 獲取形式 form=one_selector.xpath('p[1]/span/text()').extract()[0] item = QidianHotItem() #生產(chǎn)存儲器,進行信息存儲 item[’name’] = name item[’author’] = author item[’type’] = type item[’form’] = form yield item #送出信息 # 獲取下一頁URL,并生成一個request請求 self.current_page += 1 if self.current_page <= 10:#爬取前10頁next_url = 'https://www.qidian.com/rank/hotsales?style=1&page='+str(self.current_page)yield Request(url=next_url,headers=self.qidian_header,callback=self.hot_parse) def css_parse(self,response):#css定位list_selector = response.css('[class=’book-mid-info’]')for one_selector in list_selector: # 獲取小說信息 name = one_selector.css('h4>a::text').extract()[0] # 獲取作者 author = one_selector.css('.author a::text').extract()[0] # 獲取類型 type = one_selector.css('.author a::text').extract()[1] # 獲取形式 form = one_selector.css('.author span::text').extract()[0] # 定義字典 item=QidianHotItem() item[’name’]=name item[’author’] = author item[’type’] = type item[’form’] = form yield item
3.start.py(代替命令行)
在爬蟲項目文件夾下創(chuàng)建start.py。
from scrapy import cmdline#導入cmd命令窗口cmdline.execute('scrapy crawl hot -o hot.csv' .split())#運行爬蟲并生產(chǎn)csv文件
出現(xiàn)類似的過程代表爬取成功。
hot.csv
本次爬蟲內(nèi)容還是十分簡單的因為只用了spider和item,這幾乎是所有scrapy都必須調(diào)用的文件,后期還會有middlewarse.py,pipelines.py,setting.py需要編寫和配置,以及從javascript和json中提取數(shù)據(jù),難度較大。
到此這篇關于Python scrapy爬取起點中文網(wǎng)小說榜單的文章就介紹到這了,更多相關Python爬取起點中文網(wǎng)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. Spring security 自定義過濾器實現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實例代碼)2. Java8內(nèi)存模型PermGen Metaspace實例解析3. python 統(tǒng)計list中各個元素出現(xiàn)的次數(shù)的幾種方法4. ASP.NET MVC使用正則表達式驗證手機號碼5. 一文搞懂 parseInt()函數(shù)異常行為6. python學習之plot函數(shù)的使用教程7. Python 中random 庫的詳細使用8. Python 有可能刪除 GIL 嗎?9. 聊聊python在linux下與windows下導入模塊的區(qū)別說明10. Python基于百度AI實現(xiàn)抓取表情包
