python Scrapy 爬蟲框架快速入門


快速入門安裝
pip install scrapy
一、創建Scrapy項目
scrapy startproject Tencent
命令執行后,會創建一個Tencent文件夾,結構如下
ls
Tencent/
scrapy.cfg
Tencent/
  __init__.py
  items.py
  pipelines.py
  settings.py
  spiders/
    __init__.py
    爬取.py
    ....
文件說明
    scrapy.cfg 項目的配置信息,主要為Scrapy命令行工具提供一個基礎的配置信息。(真正爬蟲相關的配置信息在settings.py文件中)
    items.py 設置數據存儲模板,用於結構化數據,如:Django的Model
    pipelines 數據處理行為,如:一般結構化的數據持久化
    settings.py 配置文件,如:遞歸的層數、並發數,延遲下載等
    spiders 爬蟲目錄,如:創建文件,編寫爬蟲規則
二、編寫items.py文件,根據需要爬取的內容定義爬取字段
目標任務:爬取騰訊社招信息,需要爬取的內容為:職位名稱,職位的詳情鏈接,職位類別,招聘人數,工作地點,發布時間。
# -*- coding: utf-8 -*-
import scrapy
class TencentItem(scrapy.Item):
# 職位名
positionname = scrapy.Field()
# 詳情連接
positionlink = scrapy.Field()
# 職位類別
positionType = scrapy.Field()
# 招聘人數
peopleNum = scrapy.Field()
# 工作地點
workLocation = scrapy.Field()
# 發布時間
publishTime = scrapy.Field()
三、編寫spider文件
進入Tencent目錄,使用命令創建一個基礎爬蟲類:
# tencentPostion為爬蟲名,tencent.com為爬蟲作用范圍
scrapy genspider tencentPostion "tencent.com"
執行命令后會在spiders文件夾中創建一個tencentPostion.py的文件,現在開始對其編寫:
# -*- coding: utf-8 -*-
import scrapy
from tencent.items import TencentItem
class TencentpositionSpider(scrapy.Spider):
"""
功能:爬取騰訊社招信息
"""
# 爬蟲名
name = "tencentPosition"
# 爬蟲作用范圍
allowed_domains = ["tencent.com"]
url = "http://hr.tencent.com/position.php?&start="
offset = 0
# 起始url
start_urls = [url + str(offset)]
def parse(self, response):
for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"):
# 初始化模型對象
item = TencentItem()
# 職位名稱
item['positionname'] = each.xpath("./td[1]/a/text()").extract()[0]
# 詳情連接
item['positionlink'] = each.xpath("./td[1]/a/@href").extract()[0]
# 職位類別
item['positionType'] = each.xpath("./td[2]/text()").extract()[0]
# 招聘人數
item['peopleNum'] = each.xpath("./td[3]/text()").extract()[0]
# 工作地點
item['workLocation'] = each.xpath("./td[4]/text()").extract()[0]
# 發布時間
item['publishTime'] = each.xpath("./td[5]/text()").extract()[0]
yield item
if self.offset < 1680:
self.offset += 10
# 每次處理完一頁的數據之后,重新發送下一頁頁面請求
# self.offset自增10,同時拼接為新的url,並調用回調函數self.parse處理Response
yield scrapy.Request(self.url + str(self.offset), callback = self.parse)
四、編寫pipelines文件
# -*- coding: utf-8 -*-
import json
class TencentPipeline(object):
"""
功能:保存item數據
"""
def __init__(self):
self.filename = open("tencent.json", "w")
def process_item(self, item, spider):
text = json.dumps(dict(item), ensure_ascii = False) + ",\n"
self.filename.write(text.encode("utf-8"))
return item
def close_spider(self, spider):
self.filename.close()
五、settings文件設置(主要設置內容)
# 設置請求頭部,添加url
DEFAULT_REQUEST_HEADERS = {
"User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;",
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
}
# 設置item——pipelines
ITEM_PIPELINES = {
'tencent.pipelines.TencentPipeline': 300,
}
執行命令,運行程序
# tencentPosition為爬蟲名
scrapy crwal tencentPosition
學習https://blog.csdn.net/langshanglibie/article/details/79171657


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM