如何使用 JavaScript 将网页转换为字符串? [英] How can I get a web page into a string using JavaScript?

查看:48
本文介绍了如何使用 JavaScript 将网页转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 JavaScript 获取页面的 html 内容,该页面也可能位于另一个域中,类似于 wget 但在 JavaScript 中.我想将它用于一种网络爬虫.

I need to get the html content of a page using JavaScript, the page could be also on another domain, kind of what does wget but in JavaScript. I want to use it for a kind of web-crawler.

如果我有一个 URL,我如何使用 JavaScript 获取页面的内容并将其转换为字符串?

Using JavaScript, how can I get content of a page, provided I have an URL, and get it into a string?

推荐答案

试试这个:

function cbfunc(html) { alert(html.results[0]); }
$.getScript('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22' + 
encodeURIComponent(url) + '%22&format=xml&diagnostics=true&callback=cbfunc');

演示

更多关于 YQL

这篇关于如何使用 JavaScript 将网页转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆