机器人坐标系建立_如何在30分钟内建立一个简单的搜索机器人

机器人坐标系建立

by Quinn Langille

奎因·兰吉尔(Quinn Langille)

如何在30分钟内建立一个简单的搜索机器人 (How to Build A Simple Search Bot in 30 Minutes)

Apartment hunting sucks, especially in Montreal. This guide will show you how to build a bot that stays on top of the hunt for you. This way, you’ll never have to endlessly refresh your searches again.

公寓狩猎很糟糕,尤其是在蒙特利尔。 本指南将向您展示如何构建一个自动为您服务的机器人。 这样,您就不必再无休止地刷新搜索。

语境 (Context)

Unlike other cities, most people who rent apartments in Montreal are on the same lease term. New leases start in July, last 12 months, and end on June 30th. While one could argue that this simplifies a lot of things — such as availability and expectations — it also means that competition is steep.

与其他城市不同,大多数在蒙特利尔租房的人的租期相同。 新的租约从7月开始,历时12个月,至6月30日结束。 人们可能会说这简化了很多事情,例如可用性和期望,但这也意味着竞争非常激烈。

Every day I would wake up, refresh my 10 open Kijiji pages, and send emails inquiring about all the new ads. I would do this again at lunch, dinner, and before bed. My reply rate was low — well below 10%. When someone did reply, their answer was usually grim.

每天我都会起床,刷新10个打开的Kijiji页面,然后发送电子邮件询问所有新广告。 我会在午餐,晚餐和睡觉前再做一次。 我的回复率很低-远远低于10%。 当有人回答时,他们的回答通常很冷淡。

My next step was to up the ante and actually pick up the phone. Calling made my chances a little better. Landlords were more responsive, and this time there were usually less than 10 people ahead of me. But definitely still more than 5. Back to the drawing board.

我的下一步是举起赌注,然后才拿起电话。 打电话使我的机会更好了。 房东的React更加灵敏,这一次我前面通常只有不到10个人。 但绝对仍然超过5。回到设计图。

One day, while complaining to a coworker that all my time was getting eaten by this apartment hunt — it dawned on me. I could I solve this problem with my computer.

有一天,当我向同事抱怨说,我所有的时间都被这种打猎所吞噬时,它突然降临了。 我可以用我的计算机解决此问题。

When I got home I wrote a small program that watches Kijiji searches for changes. When it sees them, it sends a Short Message Service (SMS) text to my phone with the relevant information. The rest of this article will explain how I did that.

当我回到家时,我写了一个小程序,看着Kijiji搜索更改。 看到它们后,它将带有相关信息的短信服务(SMS)文本发送到我的手机。 本文的其余部分将解释我是如何做到的。

Note: for those who don’t care about the tutorial, I’ve put the Kijiji scraper up as an open source repo here: ?

注:对于那些谁不关心的教程中,我已经把客齐集刮了作为一个开源回购在这里

建筑护垫巡逻 (Building Pad-Patrol)

When I arrived home from work, I got my laptop out and fired up my terminal. I knew the program should be lightweight, as I’ll be running it 24/7 — or at least until I find an apartment. I decided to just build a simple node script that I could execute from my terminal.

当我下班回家时,我拿出笔记本电脑并启动了终端。 我知道该程序应该是轻量级的,因为我将全天候24/7运行它-至少直到找到公寓为止。 我决定只构建一个可以从终端执行的简单节点脚本。

建立 (Setup)

Assuming that you have node and npm installed, the first step — of any node project — is to initialize npm inside the project directory.

假设您已安装nodenpm ,则任何节点项目的第一步都是在项目目录中初始化npm。

Next, let’s create an src directory where our code will live.

接下来,让我们创建一个src目录,其中将存放我们的代码。

Inside thesrc directory, make an index.js file where our script will go.

src目录中,创建一个index.js文件,该脚本将存放在该文件中。

You can do that like so:

您可以这样做:

$ npm init // this will ask a few questions$ mkdir src$ cd src && touch index.js
编写脚本 (Writing the script)

When making a solo project I tend to freestyle — breaking stuff and then fixing it (arguably the best way to learn). I’m going to try to mimic my initial thought process with the following instructions, but let me know if they seem all over the place.

当做一个单独的项目时,我倾向于自由式-先弄碎东西再进行修复(可以说是最好的学习方法)。 我将按照以下说明尝试模仿我的初始思维过程,但请让我知道它们是否遍地都是。

The very first thing we have to do is make a successful request to Kijiji. To ensure that we’re able to get a proper response, let’s make a very basic fetch.

我们要做的第一件事就是向Kijiji发出成功的请求。 为了确保我们能够获得正确的响应,让我们进行一个非常基本的提取。

To do that, we’ll need to install a request library:

为此,我们需要安装一个请求库:

$ npm install request-promise

and then add the following to index.js:

然后将以下内容添加到index.js

Once that’s saved, we can run $ node src/index.js and we should see some HTML markup in our console. Step one complete — Easy!

保存后,我们可以运行$ node src/index.js ,我们应该在控制台中看到一些HTML标记。 完成第一步-简单!

Because we only care when content changes, lets make a simple hash of the response. That way, we can compare the response and compare the hashes. In the event we need to log our results, this will be much less cumbersome than the raw markup.

因为我们只关心内容更改的时间,所以我们对响应进行简单的哈希处理。 这样,我们可以比较响应并比较散列。 如果我们需要记录结果,那么它将比原始标记要麻烦得多。

To do this, we can use a hashing tool called checksum:

为此,我们可以使用一种称为checksum的哈希工具:

$ yarn add checksum

and then:

然后:

Ok cool, this worked! Our 1500 lines of HTML has been cut down to 32 digits. Now, let’s wrap it in a reusable function:

好的,很不错! 我们的1500行HTML已缩减为32位。 现在,让我们将其包装在可重用的函数中:

The above code will create a hash from the fetched value. Then on the following fetch, it will compare the original and new hashes.

上面的代码将根据获取的值创建一个哈希。 然后,在接下来的提取中,它将比较原始哈希和新哈希。

If they’re different, it will return true. This worked great…like, a little too great. As you’ll see, it returns true every time ?

如果它们不同,它将返回true 。 效果很好…有点太好了。 如您所见,它每次都返回true

After further inspection of the response from the fetch, we can see that Kijiji has a timestamp in the header. This means that the hash will be different on every fetch. It’s important to note that this would have also happened due to rotating ads and a bunch of other dynamic content.

进一步检查提取的响应后,我们可以看到Kijiji在标头中有一个时间戳记。 这意味着哈希在每次提取时都会有所不同。 需要特别注意的是,由于轮播广告和许多其他动态内容,这种情况也会发生。

The takeaway from the above oversight is to always carefully inspect your response when dealing with an API you didn’t write.

从上述监督中获得的好处是,在处理您未编写的API时,请务必仔细检查您的响应。

This mean’s we’ll need to access granular bits of the markup, so let’s install a third party package to help parse the response. Cheerio is a library that can ingest HTML markup and turn it into an accessible JavaScript API. It’s intended purpose was to help jQuery developers not use jQuery, but intentions are overrated.

这意味着我们需要访问标记的细粒度部分,因此让我们安装第三方软件包来帮助解析响应。 Cheerio是一个可以提取HTML标记并将其转换为可访问JavaScript API的库。 目的是为了帮助jQuery开发人员不使用jQuery ,但是意图被高估了。

For us, it’s going to be a fake set of Chrome Developer Tools!

对于我们来说,它将是一套伪造的Chrome开发者工具!

As a pre-requisite to using Cheerio in this way, we need to know what to look for in our markup. So let’s bust open Chrome and inspect our URL.

作为以这种方式使用Cheerio的先决条件,我们需要知道在标记中寻找什么。 因此,让我们打开Chrome浏览器并检查我们的URL。

If we inspect at the ads, we can see all search responses have the classes .search-item and .regular-ad. Perfect!

如果我们查看广告,则可以看到所有搜索响应都具有.regular-ad .search-item.regular-ad 。 完善!

We can select those with Cheerio like so:

我们可以像这样选择带有Cheerio的产品:

Just like we had planned, this spits out an array of neatly organized objects. According to Cheerio’s documentation, all attributes of an element are nested in a key called attribs. If we go back to the Chrome Developer Tools, we can see that each ad has a unique data-attribute called ID. Let’s target that — replace the code inside your checkURL function with the following:

就像我们计划的那样,这会吐出一系列整齐有序的对象。 根据Cheerio的文档,元素的所有属性都嵌套在称为attribs的键中。 如果回到Chrome开发者工具,我们可以看到每个广告都有一个唯一的数据属性,称为ID。 我们的目标是—用以下代码替换checkURL函数中的代码:

rp(siteToCheck).then(response => {  const $ = co.load(HTMLresponse);  let apartmentString = "";
// use cheerio to parse HTML response and find all search results  $(".search-item.regular-ad").each((i, element) => {    console.log(element.attribs["data-ad-id"]);  });});

Ok great, we’re getting a list of unique ID numbers. These ID’s are the only information we care about on the page.

好的,我们正在获得唯一ID号的列表。 这些ID是我们在页面上唯一关心的信息。

So let’s go back to our original plan of comparing hashes, except we’ll only hash the unique IDs:

因此,让我们回到比较散列的原始计划,不同的是,我们仅散列唯一的ID:

Perfect! It’s working exactly as intended. When someone posts a new ad (or removes an old ad, a caveat of watching the order of IDs) we print true in our console. All that’s left to do it set up our SMS tool.

完善! 它的工作完全符合预期。 当有人发布新广告(或删除旧广告,注意ID顺序的警告)时,我们会在控制台中打印true 。 剩下要做的一切就建立了我们的SMS工具。

从终端发送短信 (Sending SMS from the Terminal)

This is actually much easier than it seems. To do this we’ll use a third party software called Twilio. It does a lot, but one of it’s core features is to send SMS. As a bonus, it also has great JavaScript API! To finish the tutorial, you’ll need one of their accounts — a free trial will be more than enough to play around — and maybe even get a new apartment.

实际上,这比看起来容易得多。 为此,我们将使用第三方软件Twilio 。 它的功能很多,但是其核心功能之一就是发送短信。 另外,它还具有出色JavaScript API! 要完成本教程,您需要使用他们的一个帐户 -免费试用将绰绰有余,甚至可以租一个新公寓。

Ok, so to start we need to run:

好的,因此要开始,我们需要运行:

$ yarn add twilio

from there, in index.js lets add Twilio and define a new function called SMS:

从那里,在index.js可以添加Twilio并定义一个名为SMS的新函数:

const twilio = require(twilio);
// you'll need to get your own credentials for this oneconst client = new Twilio("accountID", "authKey");
function SMS({ body, to, from }) {  client.messages    .create({      body,      to,      from    })    .then(() => {      console.log(`? Success! Message has been sent to ${to}`);    })    .catch(err => {      console.log(err);    });}

This simple function takes two phone numbers (to and from) and a message (body). Instead of console logging the result of our checkURL function, we can call SMS with whatever message we want:

这个简单的函数有两个电话号码( tofrom )和消息( body )。 不用控制台记录我们的checkURL函数的结果,我们可以通过所需的任何消息来调用SMS

There you have it! Every time our script sees a change between the site hashes, it will send a text message with the URL right to your phone ?.

你有它! 每当我们的脚本看到站点哈希值之间的变化时,它将向您的手机?发送一条带有URL的文本消息。

狩猎愉快! (Happy Hunting!)

The actual script that I’ve built is a little more complicated than the above example — I’ve put it up as an open source repo on GitHub.

我构建的实际脚本比上面的示例稍微复杂-我将其作为GitHub上的开放源代码存储库放置。

Eventually, I’d like to make some additions to it — the first of which will be making it more generic and not just a Kijiji scraper. It’s pretty basic, so it will be a great first-time project for new contributors.

最终,我想对它进行一些补充-首先是使它变得更通用,而不仅仅是Kijiji刮板。 这是非常基本的,因此对于新贡献者而言,这将是一个很棒的首次项目。

Feel free to contribute in any way you see fit ?

随意以您认为合适的方式做出贡献?

Also, in case anyone was wondering, I just signed a lease last Sunday. The apartment I ended up renting was from the very first update pad-patrol sent me — it was destiny ✨

另外,如果有人想知道的话,我上周日刚刚签署了一份租约。 我最终租用的公寓是从第一次给我派出的便车巡逻队那里来的-这是命运✨

I’m currently working as a software developer at luxury fashion company in Montreal. I’ve been doing that for about a year, after finishing a web dev bootcamp last summer. I spend my free time learning hot new tech and, up until a few days ago, hunting for apartments.

我目前在蒙特利尔的豪华时尚公司担任软件开发人员。 去年夏天完成了Web开发新手训练营后,我已经做了大约一年了。 我花了空闲时间学习热门的新技术,直到几天前,我还在寻找公寓。

翻译自: https://www.freecodecamp.org/news/how-to-build-a-simple-search-bot-in-30-minutes-eb56fcedcdb1/

机器人坐标系建立

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值