刮不爬任何网页

2024-04-25 05:49:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在网站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', ...]

Tags: textdividresponseextractitemxpathclass