如何使用瀏覽器控制臺通過JavaScript抓取并將數據保存在文件中

by Praveen Dubey

通過Praveen Dubey

如何使用瀏覽器控制臺通過JavaScript抓取并將數據保存在文件中 (How to use the browser console to scrape and save data in a file with JavaScript)

A while back I had to crawl a site for links, and further use those page links to crawl data using selenium or puppeteer. Setup for the content on the site was bit uncanny so I couldn’t start directly with selenium and node. Also, unfortunately, data was huge on the site. I had to quickly come up with an approach to first crawl all the links and pass those for details crawling of each page.

前一陣子,我不得不對一個站點進行爬網以獲取鏈接,并進一步使用這些頁面鏈接來使用Selenium或puppeteer來對數據進行爬網。 該網站上的內容設置有點離奇,所以我不能直接從Selenium和Node開始。 同樣,不幸的是,該站點上的數據非常龐大。 我必須快速想出一種方法,首先抓取所有鏈接,然后將其傳遞給每個頁面的詳細信息抓取。

That’s where I learned this cool stuff with the browser Console API. You can use this on any website without much setup, as it’s just JavaScript.

那是我從瀏覽器控制臺API那里學到的好東西。 您可以在任何網站上使用它,而無需進行太多設置,因為它只是JavaScript。

Let’s jump into the technical details.

讓我們跳入技術細節。

高級概述 (High Level Overview)

For crawling all the links on a page, I wrote a small piece of JS in the console. This JavaScript crawls all the links (takes 1–2 hours, as it does pagination also) and dumps a json file with all the crawled data. The thing to keep in mind is that you need to make sure the website works similarly to a single page application. Otherwise, it does not reload the page if you want to crawl more than one page. If it does not, your console code will be gone.

為了抓取頁面上的所有鏈接,我在控制臺中編寫了一小段JS。 此JavaScript會爬網所有鏈接(需要1到2個小時,因為它也會進行分頁)并轉儲包含所有已爬網數據的json文件。 要記住的事情是,您需要確保該網站的工作方式類似于單頁應用程序。 否則,如果您要爬網多個頁面,則不會重新加載頁面 如果沒有,您的控制臺代碼將消失。

Medium does not refresh the page for some scenarios. For now, let’s crawl a story and save the scraped data in a file from the console automatically after scrapping.

中型在某些情況下不會刷新頁面。 現在,讓我們抓取一個故事,并將抓取的數據在抓取后自動從控制臺保存到文件中。

But before we do that here’s a quick demo of the final execution.

但是在開始之前,這里是最終執行的快速演示。

1.從瀏覽器獲取控制臺對象實例 (1. Get the console object instance from the browser)

// Console API to clear console before logging new data
console.API;
if (typeof console._commandLineAPI !== 'undefined') {    console.API = console._commandLineAPI; //chrome
} else if (typeof console._inspectorCommandLineAPI !== 'undefined'){    console.API = console._inspectorCommandLineAPI; //Safari
} else if (typeof console.clear !== 'undefined') {    console.API = console;
}

The code is simply trying to get the console object instance based on the user’s current browser. You can ignore and directly assign the instance to your browser.

該代碼只是試圖根據用戶當前的瀏覽器獲取控制臺對象實例。 您可以忽略實例并將其直接分配給瀏覽器。

Example, if you using Chrome, the below code should be sufficient.

例如,如果您使用Chrome ,則下面的代碼應該足夠了。

if (typeof console._commandLineAPI !== 'undefined') {    console.API = console._commandLineAPI; //chrome
}

2.定義初級助手功能 (2. Defining the Junior helper function)

I’ll assume that you have opened a Medium story as of now in your browser. Lines 6 to 12 define the DOM element attributes which can be used to extract story title, clap count, user name, profile image URL, profile description and read time of the story, respectively.

我假設您已經在瀏覽器中打開了一個中型故事。 第6至12行定義DOM元素屬性,可分別用于提取故事標題,拍手數,用戶名,個人資料圖像URL,個人資料描述和故事的讀取時間

These are the basic things which I want to show for this story. You can add a few more elements like extracting links from the story, all images, or embed links.

這些是我要為這個故事展示的基本內容。 您可以添加更多元素,例如從故事中提取鏈接,所有圖像或嵌入鏈接。

3.定義我們的高級助手功能-野獸 (3. Defining our Senior helper function — the beast)

As we are crawling the page for different elements, we will save them in a collection. This collection will be passed to one of the main functions.

當我們在頁面上搜尋不同的元素時,我們會將它們保存在集合中。 該集合將傳遞給主要功能之一。

We have defined a function name, console.save. The task for this function is to dump a csv / json file with the data passed.

我們定義了一個函數名稱console.save 。 此功能的任務是轉儲帶有所傳遞數據的csv / json文件。

It creates a Blob Object with our data. A Blob object represents a file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format.

它使用我們的數據創建一個Blob對象。 Blob對象代表不可變的原始數據的類似文件的對象。 Blob表示的數據不一定是JavaScript原生格式。

Create blob is attached to a link tag <;a> on which a click event is triggered.

創建blob附加到鏈接標簽< ; a>上,在該鏈接標簽上觸發了點擊事件。

Here is the quick demo of console.save with a small array passed as data.

這是console.save的快速演示,其中有一個作為數據傳遞的小array

Putting together all the pieces of the code, this is what we have:

將所有代碼段放在一起,這就是我們所擁有的:

  1. Console API Instance

    控制臺API實例
  2. Helper function to extract elements

    輔助函數提取元素
  3. Console Save function to create a file

    控制臺保存功能可創建文件

Let’s execute our console.save() in the browser to save the data in a file. For this, you can go to a story on Medium and execute this code in the browser console.

讓我們在瀏覽器中執行console.save()以將數據保存到文件中。 為此,您可以轉到Medium上的故事并在瀏覽器控制臺中執行此代碼。

I have shown the demo with extracting data from a single page, but the same code can be tweaked to crawl multiple stories from a publisher’s home page. Take an example of freeCodeCamp: you can navigate from one story to another and come back (using the browser’s back button) to the publisher home page without the page being refreshed.

我已經演示了從單個頁面提取數據的演示,但是可以對相同的代碼進行調整,以從發布者的主頁中抓取多個故事。 以freeCodeCamp為例 :您可以從一個故事導航到另一個故事,然后(使用瀏覽器的后退按鈕)返回到發布者主頁,而無需刷新頁面。

Below is the bare minimum code you need to extract multiple stories from a publisher’s home page.

下面是從發布者的主頁中提取多個故事所需的最低限度代碼。

Let’s see the code in action for getting the profile description from multiple stories.

讓我們看一下從多個故事中獲取個人檔案描述的代碼。

For any such type of application, once you have scrapped the data, you can pass it to our console.save function and store it in a file.

對于任何這種類型的應用程序,一旦您將數據抓取后,就可以將其傳遞給我們的console.save函數并將其存儲在文件中。

The console save function can be quickly attached to your console code and can help you to dump the data in the file. I am not saying you have to use the console for scraping data, but sometimes this will be a way quicker approach since we all are very familiar working with the DOM using CSS selectors.

控制臺保存功能可以快速附加到控制臺代碼中,并可以幫助您轉儲文件中的數據。 我并不是說您必須使用控制臺來抓取數據,但是有時這將是一種更快的方法,因為我們都非常熟悉使用CSS選擇器來處理DOM。

You can download the code from Github

您可以從Github下載代碼

Thank you for reading this article! Hope it gave you cool idea to scrape some data quickly without much setup. Hit the clap button if it enjoyed it! If you have any questions, send me an email (praveend806 [at] gmail [dot] com).
感謝您閱讀本文! 希望它為您提供了一個不錯的主意,使您無需進行太多設置即可快速抓取一些數據。 如果喜歡,請按拍手按鈕! 如果您有任何疑問,請給我發送電子郵件(praveend806 [at] gmail [dot] com)。

了解更多有關控制臺的資源: (Resources to learn more about the Console:)

Using the Console | Tools for Web Developers | Google DevelopersLearn how to navigate the Chrome DevTools JavaScript Console.developers.google.comBrowser ConsoleThe Browser Console is like the Web Console, but applied to the whole browser rather than a single content tab.developer.mozilla.orgBlobA Blob object represents a file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a…developer.mozilla.org

使用控制臺| Web開發人員工具| Google Developers 了解如何瀏覽Chrome DevTools JavaScript控制臺。 developers.google.com 瀏覽器控制臺 瀏覽器控制臺類似于Web控制臺,但應用于整個瀏覽器,而不是單個內容選項卡。 developer.mozilla.org Blob Blob對象表示不可變的原始數據的類似文件的對象。 Blob代表不一定要包含在…中的數據... developer.mozilla.org

翻譯自: https://www.freecodecamp.org/news/how-to-use-the-browser-console-to-scrape-and-save-data-in-a-file-with-javascript-b40f4ded87ef/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392736.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392736.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392736.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

poj2017

1&#xff0e;鏈接地址 https://vjudge.net/problem/POJ-2017 2&#xff0e;問題描述 Bill and Ted are taking a road trip. But the odometer in their car is broken, so they dont know how many miles they have driven. Fortunately, Bill has a working stopwatch, so t…

NFL原則告訴我們做決策的時候,試圖找到一個能解決所有問題,“大而全”的方案是不存在的。我們應當找到最關心的問題,因地制宜做出選擇。——聚焦目標,取舍有道!...

資源匱乏原則&#xff1a; 有限的資源無法滿足無窮的需要及欲望&#xff1b; 因此想要多一點的某件東西&#xff0c;意味著必須放棄一些其他的東西&#xff1b; 因為資源匱乏&#xff0c;所以我們必須做出選擇。 NFL原則&#xff1a;沒有免費午餐定理(No Free Lunch)是wolpert和…

巨無霸Win8PE X64服務器維護專用,【13年4月4日】維護版win8pe【32位+64位+純64位】(支持BIOS+EFI)...

因為單獨一個PE是不夠用的&#xff0c;已經制作了合盤&#xff0c;可BIOS啟動&#xff0c;也可EFI啟動。詳情移步》歡迎下載使用&#xff0c;覺得好的話&#xff0c;請回帖支持一下&#xff0c;您的支持&#xff0c;就是我的動力。。。。預祝大家新的一年合家歡樂&#xff01;工…

linux子線程運行的函數_Linux中線程使用詳解

4. 線程的屬性前面還說到過線程創建的時候是有屬性的&#xff0c;這個屬性由一個線程屬性對象來描述。線程屬性對象由pthread_attr_init()接口初始化&#xff0c;并由pthread_attr_destory()來銷毀&#xff0c;它們的完整定義是&#xff1a;int pthread_attr_init(pthread_attr…

數據源 連接oracle

https://blog.csdn.net/kk185800961/article/details/53065257 轉載于:https://www.cnblogs.com/BelieveFish/p/11164009.html

leetcode 140. 單詞拆分 II(記憶化)

給定一個非空字符串 s 和一個包含非空單詞列表的字典 wordDict&#xff0c;在字符串中增加空格來構建一個句子&#xff0c;使得句子中所有的單詞都在詞典中。返回所有這些可能的句子。 說明&#xff1a; 分隔時可以重復使用字典中的單詞。 你可以假設字典中沒有重復的單詞。 …

java mvp開發_如何從沒有軟件開發技能的想法變成現實的市場MVP???

java mvp開發by Mike Williams由Mike Williams 如何從沒有軟件開發技能的想法變成現實的市場MVP&#xff1f;?&#xff1f; (How to go from idea to live marketplace MVP with no software development skills ???) Online marketplaces such as Airbnb, Turo, Hipcamp,…

Convolutional neural networks for artistic style transfer

https://harishnarayanan.org/writing/artistic-style-transfer/ 轉載于:https://www.cnblogs.com/guochen/p/6888478.html

Centos 安裝 禪道

Centos 安裝 禪道 一、環境準備&#xff1a; 1、服務器&#xff1a;Centos6.7 新系統 2、查看對應的系統版本&#xff1a;uname -a和cat /etc/redhat CentOS release 6.7 (Final) 二、安裝&#xff1a; 1、下載對應系統版本的zbox禪道一鍵安裝包&#xff0c;解壓至/opt目錄下 …

centos7修改服務器密碼忘記,Centos7忘記root密碼怎么修改

Centos7忘記root密碼怎么修改一、 reboot重啟機器&#xff0c;當出現引導界面時&#xff0c;按e進入內核編輯界面。二、 往下翻&#xff0c;到LANGzh_CN.UTF-8后面添加 \rd.break(別忘了空格)三&#xff0c; 修改完成后&#xff0c;按下CtrlX組合鍵來運行這個修改后的內核程序(…

1.移動端測試知識筆記(面試必備,測試點,adb命令)

移動端測試&#xff1a; 移動應用&#xff0c;特性(功能) 滿足 需求(產品文檔&#xff0c;隱性需求) 一。App功能測試&#xff1a; 死活背下來1.業務邏輯正確性測試&#xff1a; 產品文檔&#xff0c;隱性需求- 寫成測試用例 2.兼容性測試&#xff1a; 1.系統版本&#xff1a…

Day 3 網絡基礎

網絡基礎 一、什么是互聯網協議及為何要有互聯網協議 &#xff1f; 互聯網協議&#xff1a;指的就是一系列統一的標準&#xff0c;這些標準稱之為互聯網協議。互聯網的本質就是一系列的協議&#xff0c;總稱為‘互聯網協議’&#xff08;Internet Protocol Suite)。 互聯網協議…

leetcode 349. 兩個數組的交集

給定兩個數組&#xff0c;編寫一個函數來計算它們的交集。 示例 1&#xff1a; 輸入&#xff1a;nums1 [1,2,2,1], nums2 [2,2] 輸出&#xff1a;[2] 示例 2&#xff1a; 輸入&#xff1a;nums1 [4,9,5], nums2 [9,4,9,8,4] 輸出&#xff1a;[9,4] 代碼 class Solution…

a4988 脈寬要求_基于STM32的微型步進電機驅動控制器設計

基于STM32的微型步進電機驅動控制器設計摘 要&#xff1a; 設計了一種微型步進電機驅動控制器&#xff0c;通過上位機界面修改步進電機轉速、旋轉角度、細分系數。該設計以STM32F103T8U6作為主控制器&#xff0c;以A4988步進電機驅動設備&#xff0c;上位機串口界面作為人機接口…

運行linux的機器死機了_如何在任何機器上輕松運行任何Linux工具

運行linux的機器死機了by Flavio De Stefano由弗拉維奧德斯特凡諾(Flavio De Stefano) 如何在任何機器上輕松運行任何Linux工具 (How to easily run any Linux tool on any machine) Have you ever encountered a situation like the ones below?您是否遇到過以下情況&#x…

【實戰】爛泥:一次糾結的系統安裝

這應該是昨天的事了&#xff0c;因為昨天太忙了&#xff0c;就沒有貼出來&#xff0c;今天下午我想了想還是貼出來吧。一是給自己一個提醒&#xff0c;二是也給壇子里面的午飯們再以后安裝系統中提供一種思路。 環境&#xff1a;thinkpad x100e筆記本&#xff0c;2G內存&#x…

Android動態改變TextView字體顏色

Android動態改變TextView字體顏色 分類&#xff1a; Android 2012-06-04 21:56 141人閱讀 評論(0) 收藏 舉報androidcolorslayout必須在在res/color文件夾下面創建一個selector的xml [html] view plaincopyfont_style_colors.xml <selector xmlns:android"http://…

關于小程序的一些坑的總結

最近開發的小程序&#xff0c;有很多的坑。 1.底部的tabbar 不可更改尺寸和字體的大小。限制的還是蠻死的&#xff01;不知道是不是我沒找到方法去修改還是咋的。淡淡的憂桑&#xff5e;&#xff5e;&#xff5e; 2.可以動態的設置小程序的頂部導航欄的字&#xff0c;但是不可…

開源項目貢獻者_如何認識您的開源項目貢獻者并發展您的社區

開源項目貢獻者by David Herron大衛赫倫(David Herron) 如何認識您的開源項目貢獻者并發展您的社區 (How to recognize your open source project contributors and grow your community) There’s a truism — if a community is not growing, it is slowly dying. How is yo…

華農java實驗7_國家實驗教學示范中心

我校有生物學實驗教學中心、作物學實驗教學中心、水產養殖實驗教學中心、動物醫學實驗教學中心4個國家級實驗教學示范中心&#xff0c;10個省級實驗教學示范中心。生物學實驗教學中心華中農業大學生物學實驗教學中心成立于2001年7月&#xff0c;是直屬于生命科學技術學院的校級…