从通过 XMLHTTPRequest 加载的网页中提取 URL 的最佳方法? [英] Optimal way to extract a URL from web page loaded via XMLHTTPRequest?
问题描述
问题概览
- 我有一个动态生成的网页,
X
,它由链接到网页的搜索结果组成,Y1
、Y2
、Y3
等 Y1
包含资源 URLR1
,Y2
包含资源 URLR2
,依此类推.立>- 我想动态地增强页面
X
链接到资源R1
、R2
等
- I have a dynamically produced web page,
X
, which consists of search results linking to web pages,Y1
,Y2
,Y3
etc. Y1
contains a resource URLR1
,Y2
contains resource URLR2
, and so on.- I would like to dynamically enhance page
X
with links to resourcesR1
,R2
etc.
可能的解决方案
我目前正在考虑使用 JavaScript 和 XMLHTTPRequest 从网页 Y1
、Y2
等中检索 HTML,并然后使用正则表达式提取网址.
I'm currently thinking of using JavaScript and XMLHTTPRequest to retrieve the HTML from web pages Y1
, Y2
, etc., and to then use a regular expression to extract the URL.
页面Y1
、Y2
等在每个30-100KB HTML区域内.
Pages Y1
, Y2
, etc. are in the region of 30-100KB HTML each.
这听起来是个好计划吗?或者我最好以 JSON 格式检索每个网页并从中提取资源 URL?如果 HTML 是可行的方法,您是否有任何建议的优化/捷径来搜索 30-100 KB 的文本?
Does this sound like a good plan? Or would I be better retrieving each web page in JSON format and extracting the resource URL from there? If HTML is the way to go, do you have any suggested optimisations/short cuts for searching 30-100 KB of text?
推荐答案
您不想使用正则表达式来提取 URL.我建议使用jQuery来执行AJAX请求,然后使用jQuery从服务器返回的HTML中解析过滤出URL.
You don't want to use regex to extract the URL. I suggest using jQuery to perform the AJAX request, and then use jQuery to parse and filter out the URLs from the HTML that is returned from the server.
jQuery.ajax({
url: "http://my.url.here",
dataType: "html";
...
success: function(data) {
jQuery("a", data).each(function() {
var $link = jQuery(this);
...
...
});
}
...
});
如果 jQuery 不是一个选项,您可以在收到回复时执行以下操作:
If jQuery is not an option, you can do something like this when you get your response back:
var html = XHR.responseText;
var div = document.createElement("div");
div.innerHTML = html;
//you can now search for nodes inside your div.
//The following gives you all the anchor tags
div.getElementsByTagName('a');
...
这篇关于从通过 XMLHTTPRequest 加载的网页中提取 URL 的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!