谈谈 pyspider 框架

2018-11-17 19:41:15 +08:00
 Northxw

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

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

1948 次点击
所在节点    问与答
12 条回复
kslr
2018-11-17 20:05:54 +08:00
存储部分扩展不是基本操作
Northxw
2018-11-17 20:12:49 +08:00
明示吧 大哥 不要搞暗语了
laike9m
2018-11-17 21:23:46 +08:00
@binux 你直接问作者呗
Northxw
2018-11-17 22:38:53 +08:00
@laike9m 我是一个超级无敌很害羞的男生! emmm... 我都不信
snoopy1024
2018-11-17 22:53:11 +08:00
为什么不能存?
Northxw
2018-11-17 23:01:05 +08:00
@snoopy1024 不清楚 看了下 Github 一年前就有人 issue 这问题了。。。
binux
2018-11-18 01:22:23 +08:00
599 那是 pycurl 的问题,为什么不能存 MongoDB ?
Northxw
2018-11-18 09:50:09 +08:00
@binux 老哥 存储的时候 总是存那么几条数据。 可能是我的操作不当。。。
binux
2018-11-18 10:41:22 +08:00
@Northxw 一样的 URL ?
Northxw
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
2018-11-19 11:04:38 +08:00
@Northxw #10 你直接创建的 mongodb 的连接,自己插入的数据,和 pyspider 有什么关系?
Northxw
2018-11-19 11:26:07 +08:00
@binux 打扰了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/508833

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX