微信找房机器人

发布于 2025-01-30 14:11:02 字数 2059 浏览 8 评论 0

身在帝都的人都知道租房的困难,每次找房都是心力交瘁。其中豆瓣租房小组算是比较靠谱的房源了,但是由于小组信息繁杂,而且没有搜索的功能,想要实时获取租房信息是件很困难的事情,所以最近给自己挖了个坑,做个微信找房机器人,先看大概效果吧,见下图:

WechatIMG5

实现

说下大概的技术实现吧,首先是 scrapy 爬虫对于豆瓣北京租房的小组实时爬取,存储在 mongodb 中,因为做的是全文检索,所以对 title, description 使用 jieba 和 whoosh 进行了分词和索引,做成 api。接下来就是应用的接入,网上有微信机器人的开源 wxBot ,所以对它进行了修改,实现了定时推送和持久化。最后顺便把公众号也做了同样的功能,支持实时租房信息搜索。

部分代码

scrapy 支持自定义 pipeline,能很方便的实现数据录入的时候实时生成索引,见 code:

class IndexPipeline(object):

def __init__(self, index):
self.index = index

@classmethod
def from_crawler(cls, crawler):
return cls(
index=crawler.settings.get('WHOOSH_INDEX', 'indexes')
)

def process_item(self, item, spider):
self.writer = AsyncWriter(get_index(self.index, zufang_schema))
create_time = datetime.datetime.strptime(item['create_time'], "%Y-%m-%d %H:%M:%S")
self.writer.update_document(
url=item['url'].decode('utf-8'),
title=item['title'],
description=item['description'],
create_time=create_time
)
self.writer.commit()
return item

搜索 api 代码很简单:

def zufang_query(keywords, limit=100):
ix = get_index('indexes', zufang_schema)
content = ["title", "description"]
query = MultifieldParser(content, ix.schema).parse(keywords)

result_list = []
with ix.searcher() as searcher:
results = searcher.search(query, sortedby="create_time", reverse=True, limit=limit)
for i in results:
result_list.append({'url': i['url'], 'title': i['title'], 'create_time': i['create_time']})
return result_list

微信机器人代码自己去看原作者的吧,写的挺清晰的,至于整个项目的代码现在还没整理完,整理完后考虑开源。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

征棹

暂无简介

文章
评论
29 人气
更多

推荐作者

夢野间

文章 0 评论 0

百度③文鱼

文章 0 评论 0

小草泠泠

文章 0 评论 0

zhuwenyan

文章 0 评论 0

weirdo

文章 0 评论 0

坚持沉默

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文