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.pswp.cn/news/393450.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393450.shtml
英文地址,請注明出處:http://en.pswp.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"><…