Node.js puppeteer - 使用不同的ElementTag从txt文件中获取数据

问题描述 投票:0回答:1

我正在使用node.js和puppeteer循环遍历一堆txt文件以获取一些值:

txt文件(被提取)

<ABC-DOCUMENT>520.txt
<DOCUMENT>
<TYPE>INFORMATION TABLE
<SEQUENCE>2
<FILENAME>infotable.xml
<TEXT>
<XML>
<?xml version="1.0" ?>
<informationTable xsi:schemaLocation="/document/thirteenf/informationtable" xmlns="/document/thirteenf/informationtable" xmlns:n1="/document/thirteenf/informationtable" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <infoTable>
    <valueA>Company A</valueA>
    <valueB>INC</valueB>
    <shParent>
        <valueC>123</valueC>
        <valueD>AB</valueD>
    </shParent>
    </infoTable>
    <infoTable>
    <valueA>Company B</valueA>
    <valueB>LTD</valueB>
    <shParent>
        <valueC>567</valueC>
        <valueD>ST</valueD>
    </shParent>
  </infoTable>
</informationTable>
</XML>
</TEXT>
</DOCUMENT>
</ABC-DOCUMENT> 

我使用下面的查询(Post:Node.js puppeteer - Fetching content from a complex txt file)来遍历文件:

我的剧本:

const example = await page.evaluate( () =>
{
    const page = document.createElement( 'html' );
    const page_content = document.body.textContent;

    page.innerHTML = page_content;

    return {
        'valueA' : Array.from( page.getElementsByTagName( 'valueA' ), e => e.textContent ),
        'valueB' : Array.from( page.getElementsByTagName( 'valueB' ), e => e.textContent ),
        'valueC' : Array.from( page.getElementsByTagName( 'valueC' ), e => e.textContent ),
        'valueD' : Array.from( page.getElementsByTagName( 'valueD' ), e => e.textContent )
    };
});

console.log( example.valueA[0] ); // Company A
console.log( example.valueA[1] ); // Company B

console.log( example.valueB[0] ); // INC
console.log( example.valueB[1] ); // LTD

console.log( example.valueC[0] ); // 123
console.log( example.valueC[1] ); // 567

console.log( example.valueD[0] ); // AB
console.log( example.valueD[1] ); // ST

一些文件在开头有ns1:但是:

txt文件(此刻被跳过):

<ABC-DOCUMENT>006.txt
<DOCUMENT>
<TEXT>
<XML>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:informationTable xmlns:ns1="/document/thirteenf/informationtable">
  <ns1:infoTable>
    <ns1:valueA>Company D</ns1:valueA>
    <ns1:valueB>INC</ns1:valueB>
    <ns1:shParent>
        <ns1:valueC>567</ns1:valueC>
        <ns1:valueD>AB</ns1:valueD>
    </ns1:shParent>
  </ns1:infoTable>
  <ns1:infoTable>
    <ns1:valueA>Company F</ns1:valueA>
    <ns1:valueB>Corp</ns1:valueB>
    <ns1:shParent>
        <ns1:valueC>692</ns1:valueC>
        <ns1:valueD>Ave</ns1:valueD>
    </ns1:shParent> 
  </ns1:infoTable>    
</ns1:informationTable>
</XML>
</TEXT>
</DOCUMENT>
</ABC-DOCUMENT>

因此,目前正在跳过所有这些文件。我怎样才能读取这些文件并获取值?

javascript node.js puppeteer
1个回答
1
投票

您可以使用filter()方法来帮助选择适当的元素,然后您可以使用map()方法将textContent提取回原始数组:

const example = await page.evaluate( () =>
{
    const page = document.createElement( 'html' );
    const page_content = document.body.textContent;

    page.innerHTML = page_content;

    const all_elements = Array.from( page.querySelectorAll( '*' ) );

    return {
        'valueA' : all_elements.filter( e => e.tagName.endsWith( 'VALUEA' ) ).map( e => e.textContent ),
        'valueB' : all_elements.filter( e => e.tagName.endsWith( 'VALUEB' ) ).map( e => e.textContent ),
        'valueC' : all_elements.filter( e => e.tagName.endsWith( 'VALUEC' ) ).map( e => e.textContent ),
        'valueD' : all_elements.filter( e => e.tagName.endsWith( 'VALUED' ) ).map( e => e.textContent )
    };
});
© www.soinside.com 2019 - 2024. All rights reserved.