简体   繁体   English

使用 javascript 从 html 页面获取数据

[英]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.

相关问题 如何从中获取特定数据<SCRIPT> tag in HTML page using Javascript - How to get a specific data from the <SCRIPT> tag in HTML page using Javascript 我如何使用JavaScript将jenkins数据获取到HTML页面 - How can I get jenkins data to an HTML page using javascript 使用Javascript从HTML页面到Python Flask获取textarea值 - Get textarea value from HTML page to Python Flask using Javascript 从 href 获取 html 文件并使用 vanilla javascript 将其加载到页面中 - get html file from href and load it into the page using vanilla javascript 如何使用PhantomJS从页面获取动态HTML和Javascript值 - How to get dynamic HTML and Javascript values from a page using PhantomJS 如何使用jQuery或JavaScript从html页面获取SVG代码? - How get SVG code from html page, using jQuery or JavaScript? 如何使用javascript将数据从一个html页面传输到另一页面 - how to transfer data from one html page to another using javascript 无法使用 javascript 从 html 页面中的 url 检索 JSON 数据 - Unable to retrieve JSON data from url in html page using javascript 如何从mongodb获取数据并使用nodeJS将其传输到HTML页面? - how to GET data from mongodb and transfer it to Html page using nodeJS? 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM