scrapy框架爬取51job网

# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import CrawlSpider,Rule
from scrapy.linkextractors import LinkExtractor
from manhua.items import ManhuaItem
class DemoSpider(CrawlSpider):
    name = "demo"
    #这里爬取两个网站,一个是php工程师,一个是python工  程师
    start_urls=["http://search.51job.com/list/000000,000000,0000,00,9,99,php,2,1.html?lang=c&degreefrom=99&stype=&workyear=99&cotype=99&jobterm=99&companysize=99&radius=-1&address=&lonlat=&postchannel=&list_type=&ord_field=&curr_page=&dibiaoid=0&landmark=&welfare=",
                "http://search.51job.com/list/000000,000000,0000,00,9,99,Python%25E5%25BC%2580%25E5%258F%2591%25E5%25B7%25A5%25E7%25A8%258B%25E5%25B8%2588,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="
                ]
    rules={
        Rule(LinkExtractor(allow="http:\/\/search.51job.com\/list\/",restrict_xpaths="//div[@class='p_in']"),callback="paser_item",follow=True),
        # Rule(LinkExtractor(allow=""))
    }

    def paser_item(self,response):
        divs=response.xpath("//div[@class='el']")
        item=ManhuaItem()

        for div in divs:
            try:
                item['duty'] = div.xpath("./p/span/a/text()")[0].extract().strip()
                item['time'] = div.xpath("./span[4]/text()").extract()
                item['name'] = div.xpath("./span[1]/a/text()").extract()
                item['location'] = div.xpath("./span[2]/text()").extract()
                item['sallary'] = div.xpath("./span[3]/text()").extract()
                yield item
            except Exception:
                pass

*******************items.py********************
import scrapy
class ManhuaItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()  
    name=scrapy.Field()       #公司名称
    duty=scrapy.Field()     #工作的标题
    location=scrapy.Field()     #公司地址
    sallary=scrapy.Field()     #薪水
    time=scrapy.Field()    #时间

  **********************************************
  最后在控制台运行即可:scrapy crawl demo -o file.csv


这里我大概爬了四万多条数据,如果想要数据的可以私聊我










好的,您想了解如何使用Scrapy框架爬取51job招聘站的招聘信息是吗?以下是简单的爬虫代码示例: 首先,您需要在Scrapy中创建一个新项目: ``` scrapy startproject jobspider ``` 然后,在项目目录下创建一个新的spider: ``` cd jobspider scrapy genspider job 51job.com ``` 接着,您需要在spider中定义要爬取的数据字段和要爬取页链接: ```python import scrapy class JobSpider(scrapy.Spider): name = "job" allowed_domains = ["51job.com"] start_urls = [ "https://search.51job.com/list/000000,000000,0000,00,9,99,python,2,1.html" ] def parse(self, response): for job in response.css(".el"): yield { "title": job.css(".t1 a::attr(title)").get(), "company": job.css(".t2 a::text").get(), "location": job.css(".t3::text").get(), "salary": job.css(".t4::text").get(), "date": job.css(".t5::text").get(), } next_page = response.css(".bk a:last-child::attr(href)").get() if next_page is not None: yield response.follow(next_page, self.parse) ``` 在此示例中,我们定义了爬取职位标题,公司名称,工作地点,薪水和发布日期的数据字段。start_urls中包含我们要爬取的初始址,然后在parse函数中使用css选择器筛选相应的数据,并使用response.follow方法跟进下一页链接。 最后,您可以在命令行中运行以下命令来启动爬虫: ``` scrapy crawl job -o job.csv ``` 此命令将爬取51job站上的所有招聘信息,并将结果保存在名为“job.csv”的文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不才陈某

欢迎关注公众号【码猿技术专栏】

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值