node.js web框架

So what’s web scraping anyway? It involves automating away the laborious task of collecting information from websites.

那么,什么是网络抓取? 它涉及自动化从网站收集信息的艰巨任务。

There are a lot of use cases for web scraping: you might want to collect prices from various e-commerce sites for a price comparison site. Or perhaps you need flight times and hotel/AirBNB listings for a travel site. Maybe you want to collect emails from various directories for sales leads, or use data from the internet to train machine learning/AI models. Or you could even be wanting to build a search engine like Google!

Web抓取有很多用例:您可能希望从各种电子商务网站收集价格以进行价格比较。 或者,您可能需要旅行时间的航班和酒店/ AirBNB列表。 也许您想从各个目录收集电子邮件以获取销售线索,或者使用互联网上的数据来训练机器学习/ AI模型。 或者,您甚至可能想要构建像Google这样的搜索引擎!

Getting started with web scraping is easy, and the process can be broken down into two main parts:

网页抓取很容易上手,该过程可以分为两个主要部分:

  • acquiring the data using an HTML request library or a headless browser,使用HTML请求库或无头浏览器获取数据,
  • and parsing the data to get the exact information you want.并解析数据以获得所需的确切信息。

This guide will walk you through the process with the popular Node.js request-promise module, CheerioJS, and Puppeteer. Working through the examples in this guide, you will learn all the tips and tricks you need to become a pro at gathering any data you need with Node.js!

本指南将通过流行的Node.js 请求承诺模块CheerioJS和Puppeteer引导您完成该过程。 通过阅读本指南中的示例,您将学到成为专业人士使用Node.js收集所需数据所需的所有提示和技巧!

We will be gathering a list of all the names and birthdays of U.S. presidents from Wikipedia and the titles of all the posts on the front page of Reddit.

我们将在Wikipedia上收集美国总统的所有姓名和生日的列表,以及Reddit主页上所有职位的标题。

First things first: Let’s install the libraries we’ll be using in this guide (Puppeteer will take a while to install as it needs to download Chromium as well).

首先,首先要安装我们将在本指南中使用的库(Puppeteer需要花一些时间才能安装,因为它也需要下载Chromium)。

发出第一个请求 (Making your first request)

Next, let’s open a new text file (name the file potusScraper.js), and write a quick function to get the HTML of the Wikipedia “List of Presidents” page.

接下来,让我们打开一个新的文本文件(将文件命名为potusScraper.js),并编写一个快速函数以获取Wikipedia“总统名单”页面HTML。

Output:

输出:

使用Chrome DevTools (Using Chrome DevTools)

Cool, we got the raw HTML from the web page! But now we need to make sense of this giant blob of text. To do that, we’ll need to use Chrome DevTools to allow us to easily search through the HTML of a web page.

太酷了,我们从网页上获得了原始HTML! 但是现在,我们需要弄清这一巨大的文本斑点。 为此,我们需要使用Chrome DevTools来轻松搜索网页HTML。

Using Chrome DevTools is easy: simply open Google Chrome, and right click on the element you would like to scrape (in this case I am right clicking on George Washington, because we want to get links to all of the individual presidents’ Wikipedia pages):

使用Chrome DevTools很容易:只需打开Goog​​le Chrome,然后右键单击要剪贴的元素(在这种情况下,我右键单击George Washington,因为我们希望获得指向所有总统个人维基百科页面的链接) :

Now, simply click inspect, and Chrome will bring up its DevTools pane, allowing you to easily inspect the page’s source HTML.

现在,只需单击检查,Chrome就会弹出其DevTools窗格,使您可以轻松检查页面的源HTML。

用Cheerio.js解析HTML (Parsing HTML with Cheerio.js)

Awesome, Chrome DevTools is now showing us the exact pattern we should be looking for in the code (a “big” tag with a hyperlink inside of it). Let’s use Cheerio.js to parse the HTML we received earlier to return a list of links to the individual Wikipedia pages of U.S. presidents.

太棒了,Chrome DevTools现在向我们展示了我们应该在代码中寻找的确切模式(“大”标签中带有超链接)。 让我们使用Cheerio.js解析我们之前收到HTML,以返回指向美国总统个人Wikipedia页面的链接列表。

Output:

输出:

We check to make sure there are exactly 45 elements returned (the number of U.S. presidents), meaning there aren’t any extra hidden “big” tags elsewhere on the page. Now, we can go through and grab a list of links to all 45 presidential Wikipedia pages by getting them from the “attribs” section of each element.

我们检查以确保返回的确有45个元素(美国总统的数量),这意味着页面上其他任何地方都没有多余的隐藏“大”标签。 现在,我们可以通过从每个元素的“攻击者”部分获取所有45个总统维基百科页面的链接列表。

Output:

输出:

Now we have a list of all 45 presidential Wikipedia pages. Let’s create a new file (named potusParse.js), which will contain a function to take a presidential Wikipedia page and return the president’s name and birthday. First things first, let’s get the raw HTML from George Washington’s Wikipedia page.

现在,我们列出了所有45个总统维基百科页面。 让我们创建一个新文件(名为potusParse.js),该文件将包含一个获取总统Wikipedia页面并返回总统的姓名和生日的函数。 首先,让我们从George Washington的Wikipedia页面获取原始HTML。

Output:

输出:

Let’s once again use Chrome DevTools to find the syntax of the code we want to parse, so that we can extract the name and birthday with Cheerio.js.

让我们再次使用Chrome DevTools查找我们要解析的代码的语法,以便我们可以使用Cheerio.js提取名称和生日。

So we see that the name is in a class called “firstHeading” and the birthday is in a class called “bday”. Let’s modify our code to use Cheerio.js to extract these two classes.

因此,我们看到该名称在一个名为“ firstHeading”的类中,而生日在一个名为“ bday”的类中。 让我们修改代码以使用Cheerio.js提取这两个类。

Output:

输出:

全部放在一起 (Putting it all together)

Perfect! Now let’s wrap this up into a function and export it from this module.

完善! 现在,让我们将其包装为一个函数,然后从该模块中将其导出。

Now let’s return to our original file potusScraper.js and require the potusParse.js module. We’ll then apply it to the list of wikiUrls we gathered earlier.

现在,让我们回到原始文件potusScraper.js,并需要potusParse.js模块。 然后,将其应用于我们之前收集的WikiUrl列表。

Output:

输出:

渲染JavaScript页面 (Rendering JavaScript Pages)

Voilà! A list of the names and birthdays of all 45 U.S. presidents. Using just the request-promise module and Cheerio.js should allow you to scrape the vast majority of sites on the internet.

瞧! 所有45位美国总统的姓名和生日的列表。 仅使用request-promise模块和Cheerio.js应该可以让您抓取Internet上的绝大多数站点。

Recently, however, many sites have begun using JavaScript to generate dynamic content on their websites. This causes a problem for request-promise and other similar HTTP request libraries (such as axios and fetch), because they only get the response from the initial request, but they cannot execute the JavaScript the way a web browser can.

但是,最近,许多站点已开始使用JavaScript在其网站上生成动态内容。 这对请求承诺和其他类似的HTTP请求库(例如axios和fetch)造成了问题,因为它们仅从初始请求中获取响应,但是无法像Web浏览器那样执行JavaScript。

Thus, to scrape sites that require JavaScript execution, we need another solution. In our next example, we will get the titles for all of the posts on the front page of Reddit. Let’s see what happens when we try to use request-promise as we did in the previous example.

因此,要抓取需要执行JavaScript的网站,我们需要另一个解决方案。 在下一个示例中,我们将在Reddit的首页上获得所有帖子的标题。 让我们看看当我们尝试使用上一个示例中的请求承诺时会发生什么。

Output:

输出:

Here’s what the output looks like:

输出如下所示:

Hmmm…not quite what we want. That’s because getting the actual content requires you to run the JavaScript on the page! With Puppeteer, that’s no problem.

嗯...不是我们想要的。 那是因为获取实际内容需要您在页面上运行JavaScript! 使用Puppeteer,这没问题。

Puppeteer is an extremely popular new module brought to you by the Google Chrome team that allows you to control a headless browser. This is perfect for programmatically scraping pages that require JavaScript execution. Let’s get the HTML from the front page of Reddit using Puppeteer instead of request-promise.

Puppeteer是Google Chrome小组为您带来的一种非常受欢迎的新模块,可让您控制无头浏览器。 对于以编程方式抓取需要执行JavaScript的页面而言,这是完美的选择。 让我们使用Puppeteer而不是request-promise从Reddit的首页获取HTML。

Output:

输出:

Nice! The page is filled with the correct content!

真好! 该页面填充了正确的内容!

Now we can use Chrome DevTools like we did in the previous example.

现在,我们可以像上一个示例一样使用Chrome DevTools。

It looks like Reddit is putting the titles inside “h2” tags. Let’s use Cheerio.js to extract the h2 tags from the page.

看来Reddit会将标题放在“ h2”标签中。 让我们使用Cheerio.js从页面中提取h2标签。

Output:

输出:

其他资源 (Additional Resources)

And there’s the list! At this point you should feel comfortable writing your first web scraper to gather data from any website. Here are a few additional resources that you may find helpful during your web scraping journey:

有清单! 在这一点上,您应该编写第一个Web抓取工具以从任何网站收集数据都感到很舒服。 以下是一些其他资源,在您的网络抓取过程中可能会有所帮助:

  • List of web scraping proxy services

    网络抓取代理服务列表

  • List of handy web scraping tools

    方便的网页抓取工具列表

  • List of web scraping tips

    网络抓取技巧列表

  • Comparison of web scraping proxies

    网页抓取代理的比较

  • Cheerio Documentation

    Cheerio文档

  • Puppeteer Documentation

    木偶文件

翻译自: https://www.freecodecamp.org/news/the-ultimate-guide-to-web-scraping-with-node-js-daa2027dcd3/

node.js web框架

node.js web框架_使用Node.js进行Web爬取的终极指南相关推荐

  1. Node.js Web开发_设置Node.js(1)

    电子书推荐 Multithreaded JavaScript: Concurrency Beyond the Event Loop Computers For Seniors For Dummies, ...

  2. node.js编写网页_为Node.js编写可扩展架构

    node.js编写网页 by Zafar Saleem 通过Zafar Saleem 为Node.js编写可扩展架构 (Writing Scalable Architecture For Nodejs ...

  3. node.js中模块_在Node.js中需要模块:您需要知道的一切

    node.js中模块 by Samer Buna 通过Samer Buna 在Node.js中需要模块:您需要知道的一切 (Requiring modules in Node.js: Everythi ...

  4. js list操作_使用 Node.js 实现一个命令行 todo-list(1)- 基本功能

    功能介绍 为了熟悉 Node.js,使用 Node.js 制作一个命令行小工具,项目仓库:https://github.com/FuZhouJohn/node-todo,先来介绍一下功能: 添加任务: ...

  5. 如何使用node.js后端框架中的egg.js框架

    安装egg 我们推荐直接使用脚手架,只需几条简单指令,即可快速生成项目(npm >=6.1.0): mkdir egg-example && cd egg-example npm ...

  6. 开源web框架_带有酷名称的开源JavaScript和Web框架的词汇表

    开源web框架 It's getting to the point where there are so many cool open source projects that I can't kee ...

  7. asp.net web开发框架_用Python开发一个Web框架

    一.Web框架 首先我们今天要做的事是开发一个Web框架.可能听到这你就会想.是不是很难啊?这东西自己能写出来? 如果你有这种疑惑的话,那就继续看下去吧.相信看完今天的内容你也能写出一个自己的Web框 ...

  8. node开启子线程_真Node多线程

    本文测试使用环境: 系统:macOS Mojave 10.14.2 CPU:4 核 2.3 GHz Node: 10.15.1 从 Node 线程说起 一般人理解 Node 是单线程的,所以 Node ...

  9. js input 自动换行_深入Slate.js - 拯救 ContentEditble

    我们是钉钉的文档协同团队,我们在做一些很有意义的事情,其中之一就是自研的文字编辑器.为了把自研文字编辑器做好,我们调研了开源社区各种优秀编辑器,Slate.js 是其中之一(实际上,自研文字编辑器前, ...

最新文章

  1. 互联网为什么需要全局唯一ID?
  2. openCV学习教程(一):Mat类的使用
  3. 全网最详细SpringBatch读(Reader)混合文件讲解
  4. java 创建水果_简单的java水果商店后台
  5. Mbs Framework 简介
  6. python课后作业之三科成绩总和、平均分+体脂率计算
  7. SQL 已死,NoSQL才是王道?醒醒吧,别瞎说八道了
  8. SAP FICO OAYZ配置定义资产分类中的折旧范围时,无法填写默认折旧年度和期间
  9. html5 canvas 显示文字,如何使用HTML5canvas绘制文字
  10. 三星集团继承人李在镕将接受韩国检方质询
  11. Ganymed SSH-2(ch.ethz.ssh2)
  12. 第三人称的英语作文我和我的计算机,以第三人称介绍自己的朋友英语作文
  13. 一文读懂MES与MOM
  14. 聊聊keil的外部文件引用
  15. POCO库的下载和编译
  16. python 大智慧股池_如何删除大智慧系统股票池以及运行自添加的股票池
  17. U盘无权访问,解决方法
  18. 120帧手机动态壁纸_Win10电脑也能用动态桌面了?没错,设置方法还很简单
  19. 顿悟!新手都能学懂的SpringBoot源码分析!
  20. P4343 [SHOI2015]自动刷题机

热门文章

  1. 疯狂涨知识!Java多态实现原理技术总监都拍手叫好
  2. 主成分分析(PCA)原理详解_转载
  3. 什么是 DDoS 攻击?
  4. VMWARE VCSA 6.5安装过程
  5. 搭建Maven私服那点事
  6. Android 自定义View实现QQ运动积分抽奖转盘
  7. Python模块之hashlib:提供hash算法
  8. DedeCMS 提示信息! ----------dede_addonarticle
  9. iOS开发学无止境 - NSFileManager文件操作的十个小功能
  10. 如何构建ASP.NET MVC4JQueryAJaxJSon示例