从网站提取元数据 [英] Extracting Metadata from Website

查看:130
本文介绍了从网站提取元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以让我处理html源代码,使我可以取出我想要的特定标签?

对不起,如果这听起来很简单,也很简单。

解决方案

如果您的字符串中包含HTML,那么您可以使用:

  var str ='< html>< / html>'; //你的HTML文本在这里
var div = document.createElement('div');
div.innerHTML = str;
var dom = div.firstChild; // dom是你想要的对象,
//你可以用标准的dom方法操作它



<或者,使用jQuery。 jQuery是一个库,可以帮助您更轻松地操作和访问HTML元素。首先,将其添加到文档的头部:

 < script type =text / javascriptsrc =https: //ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js\"></script> 

这是对jQuery库的引用。然后,请执行以下操作:

  var foo = $(< html>您的html位于< / html>); 

或者,如果你的html在一个变量中(例如str),你可以这样做:

  var foo = $(str); 

然后,您可以通过多种方式操作和分析foo。例如,要删除所有段落元素,可以使用

  foo.remove('p'); 

或者,要删除id =bar的段落元素,请使用:

  foo.remove('p.bar'); 

完成修改后,您可以使用以下命令获取新的html文本:

  foo.html(); 

为什么你的html在一个字符串中?它不是当前页面的html吗?


I was wondering if there's a way in javascript that allows me to process the html source code that allows me to take out specific tags that I want?

Sorry if it sounds easy or too simple. i am new to programming.

解决方案

If you have the HTML in a string, then you can use:

var str = '<html></html>'; // your html text goes here
var div = document.createElement('div');
div.innerHTML = str;
var dom = div.firstChild; // dom is the object you want,
                          // you can manipulate it using standard dom methods

Alternately, use jQuery. jQuery is a library to help you manipulate and access HTML elements more easily. First, add this to the head of your document:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

This is a reference to the jQuery library. Then, do:

var foo = $("<html>Your html here</html>");

Or, if your html is in a variable (e.g. str), you can do:

var foo = $(str);

Then, you can manipulate and parse foo in a number of ways. For example, to remove all paragraph elements, you would use

foo.remove('p');

Or, to remove the paragraph element with id="bar", use:

foo.remove('p.bar');

Once you are done your modifications, you can get the new html text using:

foo.html();

Why is your html in a string? Is it not the html of the current page?

这篇关于从网站提取元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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