Javascript 从无序列表中选择时如何最好地处理可变项目计数(和顺序)

Javascript 从无序列表中选择时如何最好地处理可变项目计数(和顺序),javascript,node.js,queryselector,Javascript,Node.js,Queryselector,我从ul元素中的列表项获取innerText属性,该元素可以有4到10个li项。 一些项目,如profilname、年龄和位置,将始终存在,其他项目,如当前学期、先前学位和其他有关学生档案的信息,可以填写,但不必填写。 因此,对于几乎每个概要文件和:nth child(x)-元素,列表的长度都会不同 将始终包含不同的信息。 我想给一个对象构造函数提供这个数据,比如说度作为第五个参数 您将如何检查列表中存在哪些信息,并为缺少的值设置一个类似“n.a.”的占位符?这是我应该在我的节点脚本中尝试的吗?

我从ul元素中的列表项获取innerText属性,该元素可以有4到10个li项。 一些项目,如profilname、年龄和位置,将始终存在,其他项目,如当前学期、先前学位和其他有关学生档案的信息,可以填写,但不必填写。 因此,对于几乎每个概要文件和:nth child(x)-元素,列表的长度都会不同 将始终包含不同的信息。 我想给一个对象构造函数提供这个数据,比如说度作为第五个参数

您将如何检查列表中存在哪些信息,并为缺少的值设置一个类似“n.a.”的占位符?这是我应该在我的节点脚本中尝试的吗?或者这是数据库中稍后的作业

我的木偶师函数通过查询选择器获取元素,以解决该问题,如下所示:

var ratingDetails = await page.evaluate(() => {

//get each element (that could be available) from a div

   let text = document.querySelector("div.report-text").innerText
   let age = document.querySelector
             ("div.card-block > ul.list-unstyled > li:nth-child(1) > span").innerText
   let sex = document.querySelector
             ("div.card-block > ul.list-unstyled > li:nth-child(2) > span").innerText      
   let startYear = document.querySelector
             ("div.card-block > ul.list-unstyled > li:nth-child(3) > span").innerText
   let studyForm = document.querySelector
             ("div.card-block > ul.list-unstyled > li:nth-child(4) > span").innerText
   let location = document.querySelector
             ("div.card-block > ul.list-unstyled > li:nth-child(5) > span").innerText
          
     [...and some more...]

    })
    
//and then use the spread syntax to fill my constructor

await ratingDetails.map(facts => new ReportObject(...facts)));

非常感谢您对如何处理该问题提出的任何建议

经过反复尝试,我想出了以下解决方案:

  • 循环无序列表中的每个li元素并获取innerText属性
  • 将步骤1中的innerText属性与模式进行比较,如果匹配,则返回一个变量(并继续下一个字符串/innerText)
  • 并从page.evaluate()函数返回包含不同信息的所有变量。 我花了相当长的时间才明白,我必须将任何计数变量传递给.evaluate()-方法才能使用其中的当前循环索引引用第n个列表元素


    超深if条件不可能是好的代码。我可能会在一个单独的问题中询问如何增强与数组的比较类型。但它是这样工作的。

    需要澄清的第一件事是
    ,因此列表对于几乎每个配置文件和第n个子项(x)都有不同的长度-元素将始终包含不同的信息。
    。我认为元素1不一定是年龄,元素2不一定是性别等。对吗?是的,对!如果有人不提供他的年龄-如果该字段已填充,这将是第一个-那么其他一些信息将是第一个li元素。您可以控制该元素吗生成列表项?每个列表项是否至少有一个数据是什么的指示器?例如,对于年龄,它实际上是说:
    Age:30
    ,还是仅仅说:
    30
    ?我无法控制列表的生成。我正在抓取一个网站(选中了terms of use and robots.txt-它是www.studycheck.de)。它说的是“年龄”:在li元素内,实际值在附加了class.value的span元素内。
    let text = [];
    for (let counter = 1; counter <= metaListe; counter++) {
      text = await page.evaluate((counter) => {
      let liElements = document.querySelector(`div.card-block > ul.list-unstyled > li:nth-child(${counter})`).innerText.trim();
      return liElements;
      }, counter);
    
    const patt_jahrStudBeginn = /^Studienbeginn/;
          const patt_abschluss = /^Abschluss/i;
          const patt_aktFS = /^Aktuelles/;
          const patt_studienForm = /^Studienform/;
          [and some more...]
    
    if(!document.querySelector(`div.card-block > ul.list-unstyled > li:nth-child(${counter})`))
        {return;}
        else{
          if(patt_studienForm.test(text)) {
            let studForm = document.querySelector(`div.card-block > ul.list-unstyled > li:nth-child(${counter}) > span`).innerText;
          }else{
            if(patt_studienDauer.test(text)) {
              let studDauer = document.querySelector(`div.card-block > ul.list-unstyled > li:nth-child(${counter}) > span`).innerText;
            }else{
              if(patt_jahrStudBeginn.test(text)) {
                let jahrBeginn = document.querySelector(`div.card-block > ul.list-unstyled > li:nth-child(${counter}) > span`).innerText;
              }else{
                if(patt_aktFS.test(text)) {
                  let aktFS = document.querySelector(`div.card-block > ul.list-unstyled > li:nth-child(${counter}) > span`).innerText;
    [...and more...]