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()

相关文章
|
3月前
|
数据采集 存储 JSON
Python网络爬虫:Scrapy框架的实战应用与技巧分享
【10月更文挑战第27天】本文介绍了Python网络爬虫Scrapy框架的实战应用与技巧。首先讲解了如何创建Scrapy项目、定义爬虫、处理JSON响应、设置User-Agent和代理,以及存储爬取的数据。通过具体示例,帮助读者掌握Scrapy的核心功能和使用方法,提升数据采集效率。
193 6
|
4月前
|
Python
Python编程获取当前日期的所属周日期信息
Python编程获取当前日期的所属周日期信息
78 1
|
1月前
|
数据采集 存储 JSON
用Scrapy精准爬取BOSS直聘特定行业职位
用Scrapy精准爬取BOSS直聘特定行业职位
|
2月前
|
JavaScript API C#
【Azure Developer】Python代码调用Graph API将外部用户添加到组,结果无效,也无错误信息
根据Graph API文档,在单个请求中将多个成员添加到组时,Python代码示例中的`members@odata.bind`被错误写为`members@odata_bind`,导致用户未成功添加。
52 10
|
3月前
|
缓存 监控 Linux
Python 实时获取Linux服务器信息
Python 实时获取Linux服务器信息
|
3月前
|
数据采集 前端开发 中间件
Python网络爬虫:Scrapy框架的实战应用与技巧分享
【10月更文挑战第26天】Python是一种强大的编程语言,在数据抓取和网络爬虫领域应用广泛。Scrapy作为高效灵活的爬虫框架,为开发者提供了强大的工具集。本文通过实战案例,详细解析Scrapy框架的应用与技巧,并附上示例代码。文章介绍了Scrapy的基本概念、创建项目、编写简单爬虫、高级特性和技巧等内容。
152 4
|
3月前
|
存储 数据采集 数据库
用 Python 爬取淘宝商品价格信息时需要注意什么?
使用 Python 爬取淘宝商品价格信息时,需注意法律和道德规范,遵守法律法规和平台规定,避免非法用途。技术上,可选择 Selenium 和 Requests 库,处理反爬措施如 IP 限制、验证码识别和请求频率控制。解析页面数据时,确定数据位置并清洗格式。数据存储可选择 CSV、Excel、JSON 或数据库,定期更新并去重。还需进行错误处理和日志记录,确保爬虫稳定运行。
|
3月前
|
数据采集 Web App开发 iOS开发
如何利用 Python 的爬虫技术获取淘宝天猫商品的价格信息?
本文介绍了使用 Python 爬虫技术获取淘宝天猫商品价格信息的两种方法。方法一使用 Selenium 模拟浏览器操作,通过定位页面元素获取价格;方法二使用 Requests 和正则表达式直接请求页面内容并提取价格。每种方法都有详细步骤和代码示例,但需注意反爬措施和法律法规。
|
4月前
|
小程序 Python
利用Python编程提取身份证的信息
利用Python编程提取身份证的信息
57 2
|
4月前
|
IDE 开发工具 数据安全/隐私保护
Python编程--实现用户注册信息写入excel文件
Python编程--实现用户注册信息写入excel文件
34 1

推荐镜像

更多