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

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框架

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393450.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java局部内部类 final_Java的局部内部类以及final类型的参数和变量

Thinking In Java里面的说法(***正确的说法): 如果定义一个匿名内部类,并且希望它使用一个在其外部定的对象,那么编译器会要求其参数引用是final 的。publicclassTester {publicstaticvoidmain(String[] args) {A a newA();C c newC();c.shou…

Vmware 安装虚拟工具 (二)

打开虚拟机,以root超级用户登陆,菜单栏选择虚拟机,install安装虚拟机 拷贝虚拟工具到 在根目录下建立文件夹,并将工具拷贝到该文件夹,例如vmtool 打开终端,进入该目录开始安装 如图,进入目录解压…

git与svn的区别 ?Git 与 SVN那个更好?

git与svn的区别 : http://www.360doc.com/content/12/1228/20/11220452_256857021.shtml 在版本控制系统的选型上,是选择Git还是SVN? 对于开源项目来说这不算问题。使用Git极大地提高了开发效率、扩大了开源项目的参与度、 增强了版本控制系统…

强化学习简介

by ADL通过ADL Reinforcement Learning is an aspect of Machine learning where an agent learns to behave in an environment, by performing certain actions and observing the rewards/results which it get from those actions.强化学习是机器学习的一个方面&#xff0…

leetcode1111. 有效括号的嵌套深度(栈)

给你一个「有效括号字符串」 seq,请你将其分成两个不相交的有效括号字符串,A 和 B,并使这两个字符串的深度最小。 不相交:每个 seq[i] 只能分给 A 和 B 二者中的一个,不能既属于 A 也属于 B 。 A 或 B 中的元素在原字…

利用Arcgis for javascript API绘制GeoJSON并同时弹出多个Popup

1.引言 由于Arcgis for javascript API不可以绘制Geojson,并且提供的Popup一般只可以弹出一个,在很多专题图制作中,会遇到不少的麻烦。因此本文结合了两个现有的Arcgis for javascript API扩充库,对其进行改造达到绘制Geojson并同…

java 线程简介_java多线程介绍

java多线程介绍多线程的基本实现进程指运行中的程序,每个进程都会分配一个内存空间,一个进程中存在多个线程,启动一个JAVA虚拟机,就是打开个一个进程,一个进程有多个线程,当多个线程同时进行,就…

webpack入门——构建简易版vue-cli

用vue-cli1/2搭建一个vue项目时,可以看到有很多关于webpack配置的文件。我们不需要知道那些繁琐的配置文件有什么作用,只需在控制台输入npm run dev,项目自动启动,我们就可以愉快的写业务代码了。 虽然vue-cli帮我们做好了一切&am…

leetcode43. 字符串相乘

给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。 示例 1: 输入: num1 “2”, num2 “3” 输出: “6” 代码 class Solution {public String multiply(String num1, String num2) {if(n…

作业二:个人博客作业内容:需求分析

作业二:个人博客作业内容:需求分析 怎样与用户有效沟通获取用户的真实需求?访谈,正式访谈系统分析员将提出一些事先准备好的具体问题;非正式访谈中,分析人员将提出一些用户可以自由回答的开放性问题&#…

HBase数据备份及恢复(导入导出)的常用方法

一、说明 随着HBase在重要的商业系统中应用的大量增加,许多企业需要通过对它们的HBase集群建立健壮的备份和故障恢复机制来保证它们的企业(数据)资产。备份Hbase时的难点是其待备份的数据集可能非常巨大,因此备份方案必须有很高的…

react和react2_为什么React16是React开发人员的福气

react和react2by Harsh Makadia通过苛刻马卡迪亚 为什么React16是React开发人员的福气 (Why React16 is a blessing to React developers) Just like how people are excited about updating their mobile apps and OS, developers should also be excited to update their fr…

jzoj4598. 【NOIP2016模拟7.9】准备食物

一个th的题(a gensokyo) 难度系数在该知识点下为$2.1$ 区间xor我们很明显会想到trie树,将每一个区间$l~r$异或和拆成$sum[l-1]$ $sum[r]$两个数的异或 注意到二进制的性质,比当前低的位即使都取1加起来都没有这位选1答案高&#x…

java number转string_Java Number类, Character类,String类

字符串在Java编程中广泛使用,字符串就是一系列字符(由一个个的字符组成)。 在Java编程语言中,字符串被视为对象。Java平台提供String类来创建和操作字符串。1. 创建字符串创建字符串的最直接方法是 -String str "Hello world!";每当它在代码中…

Android商城开发系列(二)——App启动欢迎页面制作

商城APP一般都会在应用启动时有一个欢迎界面,下面我们来实现一个最简单的欢迎页开发:就是打开商城App,先出现欢迎界面,停留几秒钟,自动进入应用程序的主界面。 首先先定义WelcomeActivity布局,布局非常简单…

DELL安装不了mysql_Windows 版本 Mysql 8.x 安装

1、官网下载安装包百度网盘链接:https://pan.baidu.com/s/1cFRbQM5720xrzMxbgjPeyA提取码:xlz72、解压安装包并新建一个文件夹作为安装目录(mysqlInstall)3、配置 Mysql 环境变量4、在解压好的目录下新建一个 my.ini 文件(注意:my.ini 文件和…

lambda 使用_如何使用Lambda和API网关构建API

lambda 使用Do you want to access your database, control your system, or execute some code from another website? An API can do all of this for you, and they’re surprisingly easy to set up.您是否要访问数据库,控制系统或从其他网站执行一些代码&…

Hyper-V Server联机调整虚拟硬盘大小

1. 技术概述: 从 Windows Server 2012 R2开始,管理员可以在运行虚拟机的同时,使用 Hyper-V 来扩展或压缩虚拟硬盘的大小。存储管理员可以通过对运行中的虚拟硬盘执行维护操作来避免代价不菲的停机。不再需要关闭虚拟机,这可以避免…

leetcode162. 寻找峰值(二分法)

峰值元素是指其值大于左右相邻值的元素。 给定一个输入数组 nums,其中 nums[i] ≠ nums[i1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。 你可以假设 nums[-1] nums[n] -…

python网络爬虫(5)BeautifulSoup的使用示范

创建并显示原始内容 其中的lxml第三方解释器加快解析速度 import bs4 from bs4 import BeautifulSoup html_str """ <html><head><title>The Dormouses story</title></head> <body> <p class"title"><…