Python爬虫 scrapy框架爬取某招聘网存入mongodb解析

yipeiwu_com5年前Python爬虫

创建项目

scrapy startproject zhaoping

创建爬虫

cd zhaoping
scrapy genspider hr zhaopingwang.com

目录结构

items.py

  title = scrapy.Field()
  position = scrapy.Field()
  publish_date = scrapy.Field()

pipelines.py

from pymongo import MongoClient

mongoclient = MongoClient(host='192.168.226.150',port=27017)
collection = mongoclient['zhaoping']['hr']

class TencentPipeline(object):
  def process_item(self, item, spider):
    print(item)
    # 需要转换为 dict
    collection.insert(dict(item))
    return item

spiders/hr.py

def parse(self, response):
    # 不要第一个 和最后一个
    tr_list = response.xpath("//table[@class='tablelist']/tr")[1:-1]
    for tr in tr_list:
      item = TencentItem()
      # xpath 从1 开始数起
      item["title"] = tr.xpath("./td[1]/a/text()").extract_first()
      item["position"] = tr.xpath("./td[2]/text()").extract_first()
      item["publish_date"] = tr.xpath("./td[5]/text()").extract_first()
      yield item

    next_url = response.xpath("//a[@id='next']/@href").extract_first()
    # 构造url
    if next_url != "javascript:;":
      print(next_url)
      next_url = "https://hr.tencent.com/" + next_url
      yield scrapy.Request(url=next_url,callback=self.parse,)

就是这么简单,就获取到数据

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python抓取百度首页的方法

本文实例讲述了python抓取百度首页的方法。分享给大家供大家参考。具体实现方法如下: import urllib def downURL(url,filename): try:...

零基础写python爬虫之爬虫框架Scrapy安装配置

零基础写python爬虫之爬虫框架Scrapy安装配置

前面十章爬虫笔记陆陆续续记录了一些简单的Python爬虫知识, 用来解决简单的贴吧下载,绩点运算自然不在话下。 不过要想批量下载大量的内容,比如知乎的所有的问答,那便显得游刃不有余了点。...

Python爬虫框架Scrapy安装使用步骤

一、爬虫框架Scarpy简介Scrapy 是一个快速的高层次的屏幕抓取和网页爬虫框架,爬取网站,从网站页面得到结构化的数据,它有着广泛的用途,从数据挖掘到监测和自动测试,Scrapy完全...

python爬虫 urllib模块url编码处理详解

案例:爬取使用搜狗根据指定词条搜索到的页面数据(例如爬取词条为‘周杰伦'的页面数据) import urllib.request # 1.指定url url = 'https://w...

python爬虫 模拟登录人人网过程解析

requests 提供了一个叫做session类,来实现客户端和服务端的会话保持 使用方法 1.实例化一个session对象 2.让session发送get或者post请求 sess...