# -*- coding: utf-8 -*-
import scrapy
from SYDW.items import SydwItem
class DanweiCrawlingSpider(scrapy.Spider):
# 继承自Spider类
name = 'danwei_crawling'
allowed_domains = ['chinasydw.org']#允许域名
start_urls = ['http://www.chinasydw.org']
base_domain = 'http://www.chinasydw.org'
def parse(self, response):
province = response.xpath("//div[@class='fenzhan']//a/@href")
for each_p in province:
yield scrapy.Request(each_p.get(),callback=self.get_page)
def get_page(self,response):
for each in response.xpath("//div[@class='body']/ul[@class = 'list11 clearfix']/li[not(@class='ivl')]"):
item = SydwItem()
name = response.xpath("//div[@class='body']/ul[@class = 'list11 clearfix']/li[not(@class='ivl')]/a[not(@style)]/text()").get()
time = response.xpath("//div[@class='body']/ul[@class = 'list11 clearfix']/li[not(@class='ivl')]/span[@class='time']/text()").get()
link = response.xpath("//div[@class='body']/ul[@class = 'list11 clearfix']/li[not(@class='ivl')]/a[not(@style)]/@href").get()
item['name']=name
item['time']=time
item['link']=link
yield item
next_url = response.xpath("//div[@class='pageset']/a[last()]/@href").get()
yield scrapy.Request(self.base_domain+next_url,callback=self.get_page,meta={'item':item})
思路:
parse函数获取分站信息
然后进入分站,获取分站每一页信息。
问题:
爬取顺序问题:
按设想应该是进入分站——爬取完多个页面——进入下一个分站——爬取完多个页面;实际上运行结果是进入分站——爬取完当前页面——进入下一个分站——爬取完当前页面,直到所有分站遍历完后再开始进入分站的下一页。
初学scrapy,希望得到大家的帮助。