本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理
以下文章來源于CSDN,作者不溫卜火
爬取前的準備
糗事百科官網:https://www.qiushibaike.com
段子網址:https://www.qiushibaike.com/text
關于決議html博主選擇的方法是使用xpath,如有不懂的同學,可看下面兩個表格,如果想要深入學習xpath的相關知識可點擊博主給的官方檔案的鏈接進行學習,博主在此宣告是為了讓讀者們能夠理解決議式的具體含義,
官網網址:https://lxml.de/tutorial.html
路徑運算式
匹配屬性
1.1 查看網頁
根據上圖示記部分可以看到我們主要的要點如下,
- 整體部分
- 作者名稱
- 文本內容
- 標簽翻頁
1.2 標簽分析
首先我們需要知道我們爬取的所有內容所在標簽
通過查看開發者選項,發現<div class ="coll old-style-coll">這個標簽對應的正是所有內容的整體存放位置,那么我們也可知道之后的所有內容都是從此標簽的子標簽內提取得到,
分析一番后,我們可以得到獲取所有文本內容的決議式如下:
//div[@class = 'col1 old-style-col1']/div
作者名稱所在位置
由上圖我們可以看到作者的位置在<h2></h2>這個標簽中,
分析一番后,我們可以得到獲取作者的決議式如下:
.//h2//text()
作者名稱所在位置
由上圖我們可以看到段子的位置在<div class ="content"></div>這個標簽中,
分析一番后,我們可以得到獲取段子的決議式如下:
.//div[@class='content']//text()
標簽翻頁
由上圖我們可以看到頁面的位置在<ul class ="pagination"></ul>這個標簽中,
分析一番后,我們可以得到獲取頁面的決議式如下:
//ul[@class='pagination']/li[last()]/a/@href
專案的具體實作
2.1 新建爬蟲專案qsbk
2.2 settings設定
在創建完成一個scrapy專案后,需要對settings進行一些修改
此處默認為True,需要修改為False,否則無法爬取內容,
取消此部分的注解并添加請求頭,偽裝自己的身份,
2.3 分別提取出作者和文本內容
查看其型別
duanzidivs = response.xpath("//div[@class = 'col1 old-style-col1']/div")
print("=")
print(type(duanzidivs))
print("=")
通過運行我們可以發現其為SelectorList型別
通過回圈遍歷分別列印出作者和文本內容
for duanzidiv in duanzidivs:
# strip() 去除前后的空白字符
author = duanzidiv.xpath(".//h2//text()").get().strip()
content = duanzidiv.xpath(".//div[@class='content']//text()").getall()
content = "".join(content).strip()
print(author)
print(content)
2.4 通過pipeline保存資料
前提準備:放開ITEM_PIPELINES的限制
第一種方式
class QsbkPipeline:
def __init__(self):
self.fp = open("duanzi.json","w",encoding="utf-8")
def open_spider(self,spider):
print('爬蟲開始了 ...')
def process_item(self, item, spider):
item_json = json.dumps(dict(item),ensure_ascii=False)
self.fp.write(item_json+'\n')
return item
def close_spider(self,spider):
self.fp.close()
print('爬蟲結束了 ...')
運行結果:
第二種方式:資料量少時使用JsonItemExporter
from scrapy.exporters import JsonItemExporter
class QsbkPipeline:
def __init__(self):
self.fp = open("duanzi.json","wb")
self.exporter = JsonItemExporter(self.fp,ensure_ascii=False,encoding='utf-8')
self.exporter.start_exporting()
def open_spider(self,spider):
print('爬蟲開始了 ...')
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
def close_spider(self,spider):
self.exporter.finish_exporting()
self.fp.close()
print('爬蟲結束了 ...')
運行結果:
第三種方式:資料量多使用JsonLinesItemExporter
from scrapy.exporters import JsonLinesItemExporter
class QsbkPipeline:
def __init__(self):
self.fp = open("duanzi.json","wb")
self.exporter = JsonLinesItemExporter(self.fp,ensure_ascii=False,encoding='utf-8')
self.exporter.start_exporting()
def open_spider(self,spider):
print('爬蟲開始了 ...')
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
def close_spider(self,spider):
self.fp.close()
print('爬蟲結束了 ...')
運行結果:
2.5 定義Item
在scrapy中不是說不能直接定義回傳字典,但是一般建議先在item中定義好然后進行呼叫
在item中分別定義author和content
class QsbkItem(scrapy.Item):
author = scrapy.Field()
content = scrapy.Field()
在qsbk_spider中也需要進行如下修改
2.6 爬取多個頁面的實作
前提準備:放開DOWNLOAD_DELAY的限制并修改為1
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 1
代碼實作
# 定義一個基本的域名
base_domain = "https://www.qiushibaike.com"
next_url = response.xpath("//ul[@class='pagination']/li[last()]/a/@href").get()
# 進行一個簡單的判斷
if not next_url:
return
else:
yield scrapy.Request(self.base_domain+next_url,callback=self.parse)
運行并查看結果:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/184129.html
標籤:其他
