在操纵up中访问子元素

时间:2018-11-28 09:09:41

标签: javascript puppeteer webautomation

我具有以下HTML结构

func InsertParamValueBlocks(params []model.ParamValueBlock) error {
	var dbWrk db.Worker
	var stmt *sql.Stmt
	var err error
	result := dbWrk.DoInTransaction(sql.LevelDefault, func() error {
		stmt, err = dbWrk.Context.Prepare(`INSERT INTO "ParamValueBlock" ("ParameterId", "DeviceId", "CompressedData", "StartDate", "UncompressedDataBits")VALUES ($1, $2, $3, $4, $5);`)
		if err != nil {
			return err
		}

		for _, param := range params {
			_, err := stmt.Exec(
				param.ParameterID,
				param.DeviceID,
				param.CompressedData,
				param.StartDate,
				param.UncompressedDataBits)
			if err != nil {
				return err
			}
		}

		return nil
	})
	//stmt close after transaction
	err = stmt.Close()
	if err != nil {
		return err
	}
	return result
}

然后我使用以下代码抓取

<div class ="container" id= "12">
    <div class="details" desc-type= "multiline">
        <a href="#">
            <div class="description"> Some Description </div>
        </a>
    </div>
</div>

如何修改上面的代码,以便可以读取const SELECTOR = "div.container"; const movies = await page.$$eval( SELECTOR, nodes => nodes.map(element => { return { movieID: element.getAttribute("id"), }; } ) ); 中的desc-type= "multiline"innerText

1 个答案:

答案 0 :(得分:1)

怎么样?

const movies = await page.$$eval(
    SELECTOR,
      nodes =>
        nodes.map(element => {
          return {
            movieID: element.getAttribute("id"),
            descType: element.querySelector('[desc-type]').getAttribute('desc-type'), 
            description: element.querySelector(".description").innerText
          };
        } )    
    );