我正在进行屏幕抓取,并且想要检索特定页面的源代码。
如何使用javascript做到这一点?请帮我。
我正在进行屏幕抓取,并且想要检索特定页面的源代码。
如何使用javascript做到这一点?请帮我。
$.ajax外部链接。这里是解决方案- stackoverflow.com/a/18447625/2657601
Answers:
简单的开始方法,尝试jQuery
$("#links").load("/Main_Page #jq-p-Getting-Started li");
jQuery Docs的更多内容
以更加结构化的方式进行屏幕抓取的另一种方法是使用YQL或Yahoo Query Language。它将返回构造为JSON或xml的抓取数据。
例如,
让我们抓取stackoverflow.com
select * from html where url="http://stackoverflow.com"
这样会给你一个JSON数组(我选择了那个选项)
"results": {
"body": {
"noscript": [
{
"div": {
"id": "noscript-padding"
}
},
{
"div": {
"id": "noscript-warning",
"p": "Stack Overflow works best with JavaScript enabled"
}
}
],
"div": [
{
"id": "notify-container"
},
{
"div": [
{
"id": "header",
"div": [
{
"id": "hlogo",
"a": {
"href": "/",
"img": {
"alt": "logo homepage",
"height": "70",
"src": "http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png",
"width": "250"
}
……..
这样做的好处是,您可以进行投影和where子句,最终使您得到的是已抓取的数据结构化的信息,而只有您需要的数据(最终使网络带宽大大减少),
例如
select * from html where url="http://stackoverflow.com" and
xpath='//div/h3/a'
会得到你
"results": {
"a": [
{
"href": "/questions/414690/iphone-simulator-port-for-windows-closed",
"title": "Duplicate: Is any Windows simulator available to test iPhone application? as a hobbyist who cannot afford a mac, i set up a toolchain kit locally on cygwin to compile objecti … ",
"content": "iphone\n simulator port for windows [closed]"
},
{
"href": "/questions/680867/how-to-redirect-the-web-page-in-flex-application",
"title": "I have a button control ....i need another web page to be redirected while clicking that button .... how to do that ? Thanks ",
"content": "How\n to redirect the web page in flex application ?"
},
…..
现在只获取我们要做的问题
select title from html where url="http://stackoverflow.com" and
xpath='//div/h3/a'
注意投影中的标题
"results": {
"a": [
{
"title": "I don't want the function to be entered simultaneously by multiple threads, neither do I want it to be entered again when it has not returned yet. Is there any approach to achieve … "
},
{
"title": "I'm certain I'm doing something really obviously stupid, but I've been trying to figure it out for a few hours now and nothing is jumping out at me. I'm using a ModelForm so I can … "
},
{
"title": "when i am going through my project in IE only its showing errors A runtime error has occurred Do you wish to debug? Line 768 Error:Expected')' Is this is regarding any script er … "
},
{
"title": "I have a java batch file consisting of 4 execution steps written for analyzing any Java application. In one of the steps, I'm adding few libs in classpath that are needed for my co … "
},
{
……
编写查询后,它会为您生成一个网址
就我们而言。
所以最终您最终会做这样的事情
var titleList = $.getJSON(theAboveUrl);
并玩。
美丽,不是吗?
只要您通过域上的代理获取要访问的任何页面,就可以使用Javascript:
<html>
<head>
<script src="/js/jquery-1.3.2.js"></script>
</head>
<body>
<script>
$.get("www.mydomain.com/?url=www.google.com", function(response) {
alert(response)
});
</script>
</body>
使用jQuery
<html>
<head>
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js" ></script>
</head>
<body>
<script>
$.get("www.google.com", function(response) { alert(response) });
</script>
</body>
我使用了ImportIO。如果您与他们建立了一个帐户(免费),则可以使用它们从任何网站请求HTML。他们让您每年最多处理5万个请求。我没有花时间寻找替代方案,但是我敢肯定有一些替代方案。
在您的Javascript中,基本上您将只发出GET请求,如下所示:
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
jsontext = request.responseText;
alert(jsontext);
}
request.open("GET", "https://extraction.import.io/query/extractor/THE_PUBLIC_LINK_THEY_GIVE_YOU?_apikey=YOUR_KEY&url=YOUR_URL", true);
request.send();
旁注:我在研究相同问题时发现了这个问题,因此其他人可能会觉得我的解决方案有所帮助。
更新:我创建了一个新产品,他们只允许我使用不到48小时,然后他们说我必须为此服务付费。如果您不付款,他们似乎很快就会关闭您的项目。我使用NodeJS和一个名为NightmareJS的库进行了自己的类似服务。您可以在此处查看其教程,并创建自己的网络抓取工具。这比较容易。我没有尝试将其设置为可以向其发出请求的API。
您可以使用FileReader API来获取文件,然后在选择文件时,将网页的网址放入选择框中。使用此代码:
function readFile() {
var f = document.getElementById("yourfileinput").files[0];
if (f) {
var r = new FileReader();
r.onload = function(e) {
alert(r.result);
}
r.readAsText(f);
} else {
alert("file could not be found")
}
}
}
尽管有许多相反的评论,但我相信使用简单的JavaScript可以克服相同的来源要求。
我并不是说以下内容是原始的,因为我相信不久前我在其他地方也看到过类似的内容。
我仅在Mac上使用Safari进行了测试。
以下演示获取基本标签中的页面并将其innerHTML移至新窗口。我的脚本添加了html标签,但是对于大多数现代浏览器,可以通过使用externalHTML避免这种情况。
<html>
<head>
<base href='http://apod.nasa.gov/apod/'>
<title>test</title>
<style>
body { margin: 0 }
textarea { outline: none; padding: 2em; width: 100%; height: 100% }
</style>
</head>
<body onload="w=window.open('#'); x=document.getElementById('t'); a='<html>\n'; b='\n</html>'; setTimeout('x.innerHTML=a+w.document.documentElement.innerHTML+b; w.close()',2000)">
<textarea id=t></textarea>
</body>
</html>
TypeError: undefined is not an object (evaluating 'w.document')