如何使用浏览器控制台通过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.mzph.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;是直属于生命科学技术学院的校级…