8

How can I get html source code from an external web page?

Something like: getHtml('http://google.com');.

I've tried to get content of iframe using: $("#frame").contents().find("html");, but if in my case the webpage doesn't have html tags, just 1 line of text. I can't do it with php file_get_contents() because it doesn't keep session data.

Jakub Hampl
  • 39,863
  • 10
  • 77
  • 106
John
  • 7,500
  • 16
  • 62
  • 95
  • Possible duplicate of [get html code using javascript with a url](http://stackoverflow.com/questions/6375461/get-html-code-using-javascript-with-a-url) – card100 Apr 12 '17 at 01:51

3 Answers3

8

Cross domain? not possible without a server process. If you are on windows and can use HTA you can use the iframe

mplungjan
  • 169,008
  • 28
  • 173
  • 236
2

With javascript, getting the file contents from another domain is not allowed in many browsers for security reasons. In php, you'd have to utilize curl.

For a workaround, you can make a php script with curl on your local domain and call that with javascript.

dialer
  • 4,348
  • 6
  • 33
  • 56
0

I think that using JavaScript and XMLHttpRequest with setRequestHeader('content-type','xml/text') and the page that you want to scrape will return the responseText as the html of the page.

chicks
  • 2,393
  • 3
  • 24
  • 40
Marwan
  • 69
  • 1
  • This answer looks incorrect. You can delete it if you like. This might be a good idea because I think you now have less than one reputation which is not good. If you delete this it might go back to one. – 700 Software Mar 15 '11 at 15:29