我正在爬网https://oa.mo.gov/personnel/classification-specifications/all。我需要到达每个职位页面,然后提取一些信息。我想我可以使用LinkExtractor或通过使用xPath查找所有URL来做到这一点,这是我在下面尝试的方法。蜘蛛程序不会显示任何错误,但也不会抓取任何页面:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from StateOfMoJDs.items import StateOfMoJDs
class StateOfMoJDs(scrapy.Spider):
name = 'StateOfMoJDs'
allowed_domains = ['oa.mo.gov']
start_urls = ['https://oa.mo.gov/personnel/classification-specifications/all']
def parse(self, response):
for url in response.xpath('//span[@class="field-content"]/a/@href').extract():
url2 = 'https://oa.mo.gov' + url
scrapy.Request(url2, callback=self.parse_job)
def parse_job(self, response):
item = StateOfMoJDs()
item["url"] = response.url
item["jobtitle"] = response.xpath('//span[@class="page-title"]/text()').extract()
item["salaryrange"] = response.xpath('//*[@id="class-spec-compact"]/div/div[1]/div[2]/div[1]/div[2]/div/text()').extract()
item["classnumber"] = response.xpath('//*[@id="class-spec-compact"]/div/div[1]/div[1]/div[1]/div/div[2]/div//text()').extract()
item["paygrade"] = response.xpath('//*[@id="class-spec-compact"]/div/div[1]/div[3]/div/div[2]/div//text()').extract()
item["definition"] = response.xpath('//*[@id="class-spec-compact"]/div/div[2]/div[1]/div[2]/div/p//text()').extract()
item["jobduties"] = response.xpath('//*[@id="class-spec-compact"]/div/div[2]/div[2]/div[2]/div/div//text()').extract()
item["basicqual"] = response.xpath('//*[@id="class-spec-compact"]/div/div[3]/div[1]/div/div//text()').extract()
item["specialqual"] = response.xpath('//*[@id="class-spec-compact"]/div/div[3]/div[2]/div[2]/div//text()').extract()
item["keyskills"] = response.xpath('//*[@id="class-spec-compact"]/div/div[4]/div/div[2]/div/div//text()').extract()
yield item
使用scrapy shell时,response.xpath('//span[@class="field-content"]/a/@href').extract()
会产生一个以逗号分隔的相对URL列表:
['/personnel/classification-specifications/3005', '/personnel/classification-specifications/3006', '/personnel/classification-specifications/3007', ...]
答案 0 :(得分:2)
在您的parse()
方法中,您需要yield
的请求:
yield scrapy.Request(url2, callback=self.parse_job)