[英]get data from html page using javascript
I have some HTML - pretty nasty, but not mine and so I don't have control over it.我有一些 HTML - 很讨厌,但不是我的,所以我无法控制它。 I need to extract some data from the form, the First name value (ABDIGANI) and the Surname value (AHMED).我需要从表单中提取一些数据,名字值 (ABDIGANI) 和姓氏值 (AHMED)。 What is the best way to do this with javascript?使用 javascript 执行此操作的最佳方法是什么?
<div class="voffset3"></div>
<div class="container well panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<span class="ax_paragraph">
First name
</span>
<div class="form-group">
<div class="ax_h5">
ABDIGANI
</div>
</div>
</div>
<div class="col-md-3">
<span class="ax_paragraph">
Surname
</span>
<div class="form-group">
<div class="ax_h5">
AHMED
</div>
</div>
</div>
</div>
</div>
</div>
</div>
var x = document.querySelectorAll(".panel-body > div >.col-md-3 > div > div"); x.forEach(myFunction); function myFunction(item, index) { //console.log(item.innerHTML.trim()); if (index===0){ console.log("First name: "+item.innerHTML.trim()); } if (index===1){ console.log("Surname: "+item.innerHTML.trim()); } }
<div class="voffset3"></div> <div class="container well panel panel-default"> <div class="panel-body"> <div class="row"> <div class="col-md-3"> <span class="ax_paragraph"> First name </span> <div class="form-group"> <div class="ax_h5"> ABDIGANI </div> </div> </div> <div class="col-md-3"> <span class="ax_paragraph"> Surname </span> <div class="form-group"> <div class="ax_h5"> AHMED </div> </div> </div> </div> </div> </div> </div>
Check this检查这个
const firstName = document.querySelector('.row .form-group div').textContent.trim();
const surname = document.querySelector('.row > div:last-child .form-group div').textContent.trim();
note : Its better to change html according to functionality needs, like if you need firstname then you must keep an id attribute to div which is having first name, same goes to surname.注意:最好根据功能需要更改 html,例如如果您需要名字,那么您必须为具有名字的 div 保留一个 id 属性,姓氏也是如此。 then select those fields using id selector, because even if you change html page structure in future, functionality will not get effected.然后 select 那些字段使用 id 选择器,因为即使您将来更改 html 页面结构,功能也不会受到影响。
Check below for reference on how the html should actually be(just to make sure you know it, but the solution you are seeking is above in first two lines) eg:查看下面的 html 实际上应该如何的参考(只是为了确保您知道它,但您正在寻找的解决方案在前两行中)例如:
<div class="voffset3"></div>
<div class="container well panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<span class="ax_paragraph">
First name
</span>
<div class="form-group">
<div class="ax_h5" id="firstNameField">
ABDIGANI
</div>
</div>
</div>
<div class="col-md-3">
<span class="ax_paragraph">
Surname
</span>
<div class="form-group">
<div class="ax_h5" id="surnameField">
AHMED
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You could consider HTML in most cases well structured.在大多数情况下,您可以考虑 HTML 结构良好。 Try this the following snippet.试试下面的代码片段。
Edit: did a change due to the first comment.编辑:由于第一条评论而进行了更改。
Edit: if you have more than one rows, you should use编辑:如果你有不止一行,你应该使用
document.querySelectorAll('.container > .panel-body > .row');
and fetch the pairs for each found element as below.并为每个找到的元素获取对,如下所示。
const markers = ['First name', 'Surname'];
const mRx = [new RegExp(markers[0]), new RegExp(markers[1])];
function findMarker(element) {
for(let i = 0; i < mRx.length; i++) {
if(element.innerHTML.match(mRx[i])) {
return markers[i];
}
}
return null;
}
function findValue(el) {
return el.parentElement.querySelector('.form-group > div').innerHTML.trim();
}
const pairs = [... document.querySelectorAll('.ax_paragraph')]
.map(el => {
return {el: el, mk: findMarker(el)};
})
.filter(n => n.mk !== null)
.map(o => {
return {key: o.mk, value: findValue(o.el)};
});
console.log(pairs);
If you can't edit the HTML, you can use the XPATH for Example.如果您无法编辑 HTML,您可以使用 XPATH 作为示例。
document.querySelector('.form-group > div').textContent but without modifying the html there is no way to distinguish first name and surname. document.querySelector('.form-group > div').textContent 但不修改 html 则无法区分名字和姓氏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.