最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get html source code from external url - Stack Overflow

programmeradmin1浏览0评论

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

Something like: getHtml('');.

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.

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.

Share Improve this question edited Mar 13, 2011 at 11:24 Jakub Hampl 40.5k10 gold badges79 silver badges111 bronze badges asked Mar 13, 2011 at 11:19 JohnJohn 7,89017 gold badges67 silver badges96 bronze badges 1
  • Possible duplicate of get html code using javascript with a url – card100 Commented Apr 12, 2017 at 1:51
Add a comment  | 

3 Answers 3

Reset to default 8

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论