我正在使用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>
我使用以下查询(发布: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>
因此,当前所有这些文件都将被跳过。我还如何读取这些文件并获取值?
答案 0 :(得分: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 )
};
});