Python爬虫:scrapy爬取腾讯社招职位信息

简介: Python爬虫:scrapy爬取腾讯社招职位信息

三个文件代码如下:

spdier.py

# -*- coding: utf-8 -*-
# author : pengshiyu
# date : 2-18-4-19
import scrapy
from scrapy.selector import Selector
from tencent_position_item import  TencentPositionItem
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
class TencentPositionSpider(scrapy.Spider):
    name = "tencent_position"
    allowed_domains = ["tencent.com"]
    custom_settings = {
        "ITEM_PIPELINES":{
            "myspider.tencent_position_spider.tencent_position_pipeline.TencentPositionPipeline": 100,
        }
    }
    start_urls =[
        "https://hr.tencent.com/position.php"
    ]
    def parse(self, response):
        base_url = "https://hr.tencent.com/"
        rows = response.css(".even, .odd")
        # 或者使用xpath解析器  或 |  ;  与 and
        # rows = response.xpath("//tr[@class='even'] | //tr[@class='odd']")
        for row in rows:
            position_name = row.xpath("./td[1]/a/text()").get()
            position_link = row.xpath("./td[1]/a/@href").get()
            position_type = row.xpath("./td[2]/text()").get()
            position_number = row.xpath("./td[3]/text()").get()
            work_location = row.xpath("./td[4]/text()").get()
            publish_time = row.xpath("./td[5]/text()").get()
            # 输出提取的信息
            print "*"*30
            print position_name
            print position_link
            print position_type
            print position_number
            print work_location
            print publish_time
            # 保存到item
            item = TencentPositionItem()
            item["position_name"] = position_name
            item["position_link"] = base_url + position_link
            item["position_type"] = position_type
            item["position_number"] = position_number
            item["work_location"] = work_location
            item["publish_time"] = publish_time
            yield item
        # 翻页, 下一页
        # 方式1 正则匹配, 可能有的版本不能用re_first,那就用re
        regex = u'<a href="([^<]*)" id="next">下一页</a>'
        ret = Selector(response).re_first(regex, replace_entities=False)
        # 方式2 css选择器查找
        next_url = response.css("#next::attr(href)").extract_first()
        if next_url != u"javascript:;":
            next_url = base_url + next_url
            print "下一页:", next_url
            yield scrapy.Request(url=next_url, callback=self.parse)
        else:
            print "最后一页了", next_url

item.py

# -*- coding:utf-8 -*-
import scrapy
class TencentPositionItem(scrapy.Item):
    position_name = scrapy.Field() # 职位名称
    position_link = scrapy.Field() # 职位链接详情
    position_type = scrapy.Field()  # 职位类型
    position_number = scrapy.Field() # 职位数量
    work_location = scrapy.Field()  # 工作地点
    publish_time = scrapy.Field()  # 发布时间

pipline.py

# -*- coding: utf-8 -*-
import json
import os
BASE_DIR = os.path.abspath(__file__)
class TencentPositionPipeline(object):
    def __init__(self):
        self.f = open("tencent_position.txt", "w")
        self.count = 0
    def process_item(self, item, spider):
        content = json.dumps(dict(item), ensure_ascii=False)+"\n"
        self.f.write(content)
        self.count += 1
        return item
    def close_spider(self, spider):
        print "爬取信息条数:{count}".format(count=self.count)
        self.f.close()

相关文章
|
数据采集 测试技术 C++
无headers爬虫 vs 带headers爬虫:Python性能对比
无headers爬虫 vs 带headers爬虫:Python性能对比
|
数据采集 存储 JSON
Python网络爬虫:Scrapy框架的实战应用与技巧分享
【10月更文挑战第27天】本文介绍了Python网络爬虫Scrapy框架的实战应用与技巧。首先讲解了如何创建Scrapy项目、定义爬虫、处理JSON响应、设置User-Agent和代理,以及存储爬取的数据。通过具体示例,帮助读者掌握Scrapy的核心功能和使用方法,提升数据采集效率。
786 6
|
机器学习/深度学习 数据采集 数据可视化
基于爬虫和机器学习的招聘数据分析与可视化系统,python django框架,前端bootstrap,机器学习有八种带有可视化大屏和后台
本文介绍了一个基于Python Django框架和Bootstrap前端技术,集成了机器学习算法和数据可视化的招聘数据分析与可视化系统,该系统通过爬虫技术获取职位信息,并使用多种机器学习模型进行薪资预测、职位匹配和趋势分析,提供了一个直观的可视化大屏和后台管理系统,以优化招聘策略并提升决策质量。
1351 4
|
数据采集 存储 JSON
从零到一构建网络爬虫帝国:HTTP协议+Python requests库深度解析
【7月更文挑战第31天】在网络数据的海洋中,使用Python的`requests`库构建网络爬虫就像探索未知的航船。HTTP协议指导爬虫与服务器交流,收集信息。HTTP请求包括请求行、头和体,响应则含状态行、头和体。`requests`简化了发送各种HTTP请求的过程。
434 4
|
数据采集 存储 搜索推荐
打造个性化网页爬虫:从零开始的Python教程
【8月更文挑战第31天】在数字信息的海洋中,网页爬虫是一艘能够自动搜集网络数据的神奇船只。本文将引导你启航,用Python语言建造属于你自己的网页爬虫。我们将一起探索如何从无到有,一步步构建一个能够抓取、解析并存储网页数据的基础爬虫。文章不仅分享代码,更带你理解背后的逻辑,让你能在遇到问题时自行找到解决方案。无论你是编程新手还是有一定基础的开发者,这篇文章都会为你打开一扇通往数据世界的新窗。
|
数据采集 存储 监控
Python 原生爬虫教程:网络爬虫的基本概念和认知
网络爬虫是一种自动抓取互联网信息的程序,广泛应用于搜索引擎、数据采集、新闻聚合和价格监控等领域。其工作流程包括 URL 调度、HTTP 请求、页面下载、解析、数据存储及新 URL 发现。Python 因其丰富的库(如 requests、BeautifulSoup、Scrapy)和简洁语法成为爬虫开发的首选语言。然而,在使用爬虫时需注意法律与道德问题,例如遵守 robots.txt 规则、控制请求频率以及合法使用数据,以确保爬虫技术健康有序发展。
1732 31
|
数据采集 存储 Web App开发
自动化爬虫:requests定时爬取前程无忧最新职位
自动化爬虫:requests定时爬取前程无忧最新职位
|
数据采集 存储 NoSQL
分布式爬虫去重:Python + Redis实现高效URL去重
分布式爬虫去重:Python + Redis实现高效URL去重
|
数据采集 存储 数据挖掘
深入探索 Python 爬虫:高级技术与实战应用
本文介绍了Python爬虫的高级技术,涵盖并发处理、反爬虫策略(如验证码识别与模拟登录)及数据存储与处理方法。通过asyncio库实现异步爬虫,提升效率;利用tesseract和requests库应对反爬措施;借助SQLAlchemy和pandas进行数据存储与分析。实战部分展示了如何爬取电商网站的商品信息及新闻网站的文章内容。提醒读者在实际应用中需遵守法律法规。
647 66
|
数据采集 XML 存储
Headers池技术在Python爬虫反反爬中的应用
Headers池技术在Python爬虫反反爬中的应用

推荐镜像

更多