二、scrapy爬虫框架——scrapy构造并发送请求
生活随笔
收集整理的這篇文章主要介紹了
二、scrapy爬虫框架——scrapy构造并发送请求
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
scrapy數(shù)據(jù)建模與請求
學(xué)習(xí)目標(biāo):
1. 數(shù)據(jù)建模
通常在做項(xiàng)目的過程中,在items.py中進(jìn)行數(shù)據(jù)建模
1.1 為什么建模
1.2 如何建模
在items.py文件中定義要提取的字段:
class MyspiderItem(scrapy.Item): name = scrapy.Field() # 講師的名字title = scrapy.Field() # 講師的職稱desc = scrapy.Field() # 講師的介紹1.3 如何使用模板類
模板類定義以后需要在爬蟲中導(dǎo)入并且實(shí)例化,之后的使用方法和使用字典相同
job.py:
from myspider.items import MyspiderItem # 導(dǎo)入Item,注意路徑 ...def parse(self, response)item = MyspiderItem() # 實(shí)例化后可直接使用item['name'] = node.xpath('./h3/text()').extract_first()item['title'] = node.xpath('./h4/text()').extract_first()item['desc'] = node.xpath('./p/text()').extract_first()print(item)注意:
1.4 開發(fā)流程總結(jié)
scrapy startproject 項(xiàng)目名
在items.py文件中進(jìn)行建模
3.1 創(chuàng)建爬蟲
scrapy genspider 爬蟲名 允許的域
3.2 完成爬蟲
修改start_urls
檢查修改allowed_domains
編寫解析方法
在pipelines.py文件中定義對數(shù)據(jù)處理的管道
在settings.py文件中注冊啟用管道
2. 翻頁請求的思路
對于要提取如下圖中所有頁面上的數(shù)據(jù)該怎么辦?
回顧requests模塊是如何實(shí)現(xiàn)翻頁請求的:
scrapy實(shí)現(xiàn)翻頁的思路:
3. 構(gòu)造Request對象,并發(fā)送請求
3.1 實(shí)現(xiàn)方法
- callback:指定解析函數(shù)名稱,表示該請求返回的響應(yīng)使用哪一個(gè)函數(shù)進(jìn)行解析
3.2 網(wǎng)易招聘爬蟲
通過爬取網(wǎng)易招聘的頁面的招聘信息,學(xué)習(xí)如何實(shí)現(xiàn)翻頁請求
地址:https://hr.163.com/position/list.do
思路分析:
注意:
3.3 代碼實(shí)現(xiàn)
在爬蟲文件的parse方法中:
......# 提取下一頁的hrefnext_url = response.xpath('//a[contains(text(),">")]/@href').extract_first()# 判斷是否是最后一頁if next_url != 'javascript:void(0)':# 構(gòu)造完整urlurl = 'https://hr.163.com/position/list.do' + next_url# 構(gòu)造scrapy.Request對象,并yield給引擎# 利用callback參數(shù)指定該Request對象之后獲取的響應(yīng)用哪個(gè)函數(shù)進(jìn)行解析yield scrapy.Request(url, callback=self.parse) ......3.4 scrapy.Request的更多參數(shù)
scrapy.Request(url[,callback,method="GET",headers,body,cookies,meta,dont_filter=False])參數(shù)解釋
4. meta參數(shù)的使用
meta的作用:meta可以實(shí)現(xiàn)數(shù)據(jù)在不同的解析函數(shù)中的傳遞
在爬蟲文件的parse方法中,提取詳情頁增加之前callback指定的parse_detail函數(shù):
def parse(self,response):...yield scrapy.Request(detail_url, callback=self.parse_detail,meta={"item":item}) ...def parse_detail(self,response):#獲取之前傳入的itemitem = resposne.meta["item"]特別注意
小結(jié)
參考代碼
wangyi/spiders/job.py
import scrapy from wangyi.items import WangyiItemclass JobSpider(scrapy.Spider):name = 'job'# 2、檢查修改域名allowed_domains = ['163.com']# 1、修改起始urlstart_urls = ['https://hr.163.com/position/list.do']# 3、解析數(shù)據(jù)def parse(self, response):# 提取數(shù)據(jù)# 獲取所有職位節(jié)點(diǎn)列表node_list = response.xpath('//*[@class="position-tb"]/tbody/tr')# print(len(node_list))# 遍歷節(jié)點(diǎn)列表,通過枚舉來過濾掉沒用的節(jié)點(diǎn)for num,node in enumerate(node_list):# print(num,node)# 設(shè)置過濾條件,將目標(biāo)節(jié)點(diǎn)獲取出來if num % 2 ==0:item = WangyiItem()item['name'] = node.xpath('./td[1]/a/text()').extract_first()# item['link'] = "https://hr.163.com/" + node.xpath('./td[1]/a/@href').extract_first()# response.urljoin()用來拼接相對路徑的url,可以理解成自動(dòng)補(bǔ)全item['link'] = response.urljoin(node.xpath('./td[1]/a/@href').extract_first())item['depart'] = node.xpath('./td[2]/text()').extract_first()item['category'] = node.xpath('./td[3]/text()').extract_first()item['type'] = node.xpath('./td[4]/text()').extract_first()item['address'] = node.xpath('./td[5]/text()').extract_first()# strip()方法去掉字符串前后的空格item['num'] = node.xpath('./td[6]/text()').extract_first().strip()item['date'] = node.xpath('./td[7]/text()').extract_first()# print(item)# yield item# 構(gòu)建詳情頁面的請求yield scrapy.Request(url = item['link'],callback = self.parse_detail,meta = {'item':item})# 模擬翻頁part_url = response.xpath('/html/body/div[2]/div[2]/div[2]/div/a[last()]/@href').extract_first()# 判斷終止條件if part_url != 'javascript:void(0)':next_url = response.urljoin(part_url)# 構(gòu)建請求對象,并且返回給引擎yield scrapy.Request(url = next_url,callback = self.parse)# 定義詳情頁面的數(shù)據(jù)解析方法def parse_detail(self,response):# 將meta傳參獲取# print('------------------',response.meta['item'])item = response.meta['item']# 提取剩余字段數(shù)據(jù)item['duty'] = ''.join(response.xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[1]/div/text()').extract()).strip()item['require'] = ''.join(response.xpath('/html/body/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/div/text()').extract()).strip()# print(item)# 返回給引擎yield itemwangyi/items.py
# Define here the models for your scraped items # # See documentation in: # https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapyclass WangyiItem(scrapy.Item):# define the fields for your item here like:name = scrapy.Field() # 職位名稱link = scrapy.Field() # 點(diǎn)擊鏈接depart = scrapy.Field() # 所屬部門category = scrapy.Field() # 職位類型type = scrapy.Field() # 工作類型address = scrapy.Field() # 工作地點(diǎn)num = scrapy.Field() # 招聘人數(shù)date = scrapy.Field() # 發(fā)布時(shí)間duty = scrapy.Field() # 崗位描述require = scrapy.Field() # 崗位要求總結(jié)
以上是生活随笔為你收集整理的二、scrapy爬虫框架——scrapy构造并发送请求的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中的threading_py
- 下一篇: 论文阅读:Spatial Transfo