2

I would like to get html contents in a webpage for processing. Then use those keywords in that webpage to be search on Google. Can you please tell me how can I get html code for the following example:

<html>
<head>
<script>

  //Here I would like to read news.yahoo.com page and get the html content of that page for further processing

var a = window.open("https://www.google.com/#q=keyword1");  // search keyword in google
setTimeout(function() { a.close() }, 100);

var b = window.open("https://www.google.com/#q=keyword2");  // search keyword in google
setTimeout(function() { b.close() }, 100);

</script>
</head>
<body>
</body>
</html>
D P.
  • 1,039
  • 7
  • 27
  • 56
  • The only way you're going to be able to perform a cross-domain HTTP request with JavaScript is if you're using a JavaScript environment that has the privileges to make such a request. Typically that would be server-side JavaScript, such as Node, but you could theoretically call your JavaScript from a bookmarklet running on a page within the domain. – zzzzBov Sep 30 '13 at 23:50

3 Answers3

1

This type of thing you would typically want to do on the server in some kind of background job. If you do it on the client, your users will have to wait for your webpage to load, then for each of the subsequent webpages to load (in windows a and b in your example), then for you to process those scripts, etc.

You can get the html of a page using a variety of different libraries, depending on the server-side language you're using. From your question history, I assume you're using PHP; see this question (and its answers) as an example.

Community
  • 1
  • 1
Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
0

As zzzBov pointed out, if you want to use javascript you need to use node.js in conjunction with PhantomJS or CasperJS.

PhantomJS and CasperJS are headless browsers, that allow you to use javascript selectors to scrape a website do whatever you want with it.

You can also use PHP, Python, Ruby or whatever programming language you're proficient with, but you'll need to use a server-side language.

ILikeTacos
  • 17,464
  • 20
  • 58
  • 88
0

The most elegant solution to this kind of a problem would be to use server side web services to retrieve and process your web page data (from news.yahoo.com etc) and present it to the client using XML or JSON.

research on JQuery AJAX

Ahsan
  • 2,488
  • 2
  • 22
  • 44