V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Northxw
V2EX  ›  问与答

谈谈 pyspider 框架

  •  
  •   Northxw · 2018-11-17 19:41:15 +08:00 · 1931 次点击
    这是一个创建于 1959 天前的主题,其中的信息可能已经有所发展或是发生改变。

    pyspider 的方便之处是毋庸置疑的,然后先不说司空见惯的 599 问题,不能存储 MongoDB 是个怎么回事?

    -- 纪念半年后再次踏进 V 社区!来自一个 ZZ 的疑问。

    12 条回复    2018-11-19 11:26:07 +08:00
    kslr
        1
    kslr  
       2018-11-17 20:05:54 +08:00
    存储部分扩展不是基本操作
    Northxw
        2
    Northxw  
    OP
       2018-11-17 20:12:49 +08:00
    明示吧 大哥 不要搞暗语了
    laike9m
        3
    laike9m  
       2018-11-17 21:23:46 +08:00 via Android
    @binux 你直接问作者呗
    Northxw
        4
    Northxw  
    OP
       2018-11-17 22:38:53 +08:00
    @laike9m 我是一个超级无敌很害羞的男生! emmm... 我都不信
    snoopy1024
        5
    snoopy1024  
       2018-11-17 22:53:11 +08:00 via Android
    为什么不能存?
    Northxw
        6
    Northxw  
    OP
       2018-11-17 23:01:05 +08:00
    @snoopy1024 不清楚 看了下 Github 一年前就有人 issue 这问题了。。。
    binux
        7
    binux  
       2018-11-18 01:22:23 +08:00 via Android
    599 那是 pycurl 的问题,为什么不能存 MongoDB ?
    Northxw
        8
    Northxw  
    OP
       2018-11-18 09:50:09 +08:00
    @binux 老哥 存储的时候 总是存那么几条数据。 可能是我的操作不当。。。
    binux
        9
    binux  
       2018-11-18 10:41:22 +08:00 via Android
    @Northxw 一样的 URL ?
    Northxw
        10
    Northxw  
    OP
       2018-11-18 14:42:46 +08:00
    @binux 我不知道是不是自己操作不当,给你看看程序。而且,我还需要老哥给我说说怎么配 json,我配了之后,运行程序就报错死循环。
    from pyspider.libs.base_handler import *
    from pymongo import MongoClient
    import time


    class Mongo(object):
    def __init__(self):
    # 初始化数据库
    self.client = MongoClient()
    self.db = self.client['lagou']
    self.collection = self.db['python']

    def insert(self, data):
    # 将字典数据插入到数据库
    if data:
    self.collection.insert(data)

    def __del__(self):
    # 关闭数据库连接
    self.client.close()

    class Handler(BaseHandler):
    crawl_config = {
    'headers': {
    'Host': 'www.lagou.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
    },
    'mongo': Mongo(),
    }

    @every(minutes=24 * 60)
    def on_start(self):
    self.crawl('https://www.lagou.com/zhaopin/Python/', callback=self.index_page, validate_cert=False,
    params={'labelWords': 'label'})

    # 设置任务有效期为两个小时(因为一般为 30 个页面左右)
    @config(age=2 * 60 * 60)
    def index_page(self, response):
    for each in response.doc('.position_link').items():
    self.crawl(each.attr.href, callback=self.detail_page, validate_cert=False)
    time.sleep(1)
    # 获取下一页链接
    next = response.doc('.item_con_pager a:last-child').attr.href
    self.crawl(next, callback=self.index_page, validate_cert=False)

    @config(priority=2)
    def detail_page(self, response):
    return {
    "company": response.doc('.job-name > .company').text(),
    "job": response.doc('.job-name > .name').text(),
    "salary": response.doc('.salary').text(),
    "other": response.doc('.job_request span').text().split('/')[1:-1],
    "labels": response.doc('.job_request li').text(),
    "publish_time": "".join(response.doc('.publish_time').text().split()),
    "job_advantage": response.doc('.job-advantage > p').text(),
    "job_description": response.doc('.job_bt p').text(),
    "work_address": response.doc('.work_addr').text().replace('查看地图', '')
    }

    def on_result(self, data):
    self.crawl_config['mongo'].insert(data)
    binux
        11
    binux  
       2018-11-19 11:04:38 +08:00
    @Northxw #10 你直接创建的 mongodb 的连接,自己插入的数据,和 pyspider 有什么关系?
    Northxw
        12
    Northxw  
    OP
       2018-11-19 11:26:07 +08:00
    @binux 打扰了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2826 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 13:16 · PVG 21:16 · LAX 06:16 · JFK 09:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.