jQuery 如何使用 javascript 从网站中提取数据。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19181901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:23:27  来源:igfitidea点击:

How do I extract data from a website using javascript.

javascriptjqueryweb-crawler

提问by Vivek

Hi complete newbie here so bear with me. Seems like a simple job but I can't seem to find an easy way to do this.

嗨,这里是新手,所以请耐心等待。似乎是一项简单的工作,但我似乎找不到一种简单的方法来做到这一点。

So I need to extract a particular text from a webpage "www.example.com/index.php". I know that the text would be available in p tag with certain id. How do I extract this data out using javascript?

所以我需要从网页“www.example.com/index.php”中提取特定文本。我知道文本将在具有特定 id 的 p 标签中可用。如何使用 javascript 提取这些数据?

What I'm trying currently is that I have my javascript file (trying.js) on my computer with the following code:

我目前正在尝试的是,我的计算机上有我的 javascript 文件(trying.js),其中包含以下代码:

$(document).ready(function () {
    $.get("www.example.com/index.php", function(data) {
        console.log(data)
    }) ;
});

and a html that runs the javascript file.

和运行 javascript 文件的 html。

When I open this html page with firefox it doesn't show me anything in console. How do I get the website's data? Am I on the correct track here? Is there a better way to do this?

当我用 Firefox 打开这个 html 页面时,它在控制台中没有显示任何内容。我如何获取网站的数据?我在正确的轨道上吗?有一个更好的方法吗?

回答by Orlando

What you're looking for is a page scraper. Javascript can't pull it off because it can only gather data from the domain you're on.

您正在寻找的是页面抓取工具。Javascript 无法实现,因为它只能从您所在的域中收集数据。

You could build it in Ruby, for example, and use one of the many existing gems for this sort of task, like https://github.com/assaf/scrapior http://nokogiri.org/

例如,您可以在 Ruby 中构建它,并使用众多现有 gem 之一来完成此类任务,例如https://github.com/assaf/scrapihttp://nokogiri.org/

回答by Dropout

Please take a look at Can Javascript read the source of any web page?

请看一下Javascript 可以读取任何网页的源代码吗?

There are multiple ways discussed. Hope it helps you.

讨论了多种方式。希望对你有帮助。