在Pupeteer Node.js中获取href属性

时间:2019-03-28 00:28:40

标签: node.js puppeteer

我知道用于捕获evaluate中元素的常用方法,例如puppeteer,但我很好奇为什么无法像JavaScript那样获得href属性

const page = await browser.newPage();

await page.goto('https://www.example.com');

let links = await page.$$('a');
for (let i = 0; i < links.length; i++) {
  console.log(links[i].getAttribute('href'));
  console.log(links[i].href);
}

4 个答案:

答案 0 :(得分:2)

我不知道为什么会这么痛,但这是我前一段时间遇到的。

async function getHrefs(page, selector) {
  return await page.$$eval(selector, anchors => [].map.call(anchors, a => a.href));
}

答案 1 :(得分:2)

await page.$$('a')返回带有ElementHandles的数组-这些对象具有自己的特定于Pupeteer的API,它们没有用于HTML元素或DOM节点的常用DOM API。因此,您需要通过page.evaluate()在浏览器上下文中检索属性/属性,或者使用相当复杂的ElementHandles API。这是两种方式的示例:

'use strict';

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch();
    const [page] = await browser.pages();

    await page.goto('https://example.org/');

    // way 1
    const hrefs1 = await page.evaluate(
      () => Array.from(
        document.querySelectorAll('a[href]'),
        a => a.getAttribute('href')
      )
    );

    // way 2
    const elementHandles = await page.$$('a');
    const propertyJsHandles = await Promise.all(
      elementHandles.map(handle => handle.getProperty('href'))
    );
    const hrefs2 = await Promise.all(
      propertyJsHandles.map(handle => handle.jsonValue())
    );

    console.log(hrefs1, hrefs2);

    await browser.close();
  } catch (err) {
    console.error(err);
  }
})();

答案 2 :(得分:0)

一种类型安全的方式,通过使用TypeScript用户的HTMLLinkElement泛型进行转换,以字符串形式返回链接的href的字符串:

await page.$$eval('a', (anchors) => anchors.map((link) => (link as HTMLLinkElement).href));

答案 3 :(得分:0)

def port_ret(weights):
    return ret.dot(weights.T).mean() * 252
# calculate annualized portfolio volatility (based on weights)

def port_vol(weights):
    return ret.dot(weights.T).std() * np.sqrt(252)


# define function to be minimized (sco only supports minimize, not maximize)
# -> maximize sharpe ratio == minimize sharpe ratio * (-1)
def min_func_sharpe(weights):
    return ((rf - port_ret(weights)) / port_vol(weights)) * -1  # sharpe ratio *


num_stocks = float(len(stocks.columns))
num_stock = len(stocks.columns)
init_weights = []
ueight = float(1/num_stocks)

for i in range(num_stock):
    init_weights.append(ueight)


# bounds: all weights shall be between 0 and 1 -> can be changed
bnds = tuple((0, 1) for i in range(num_stock))

# constraint: weights must sum up to 1 -> sum of weights - 1 = 0
cons = ({"type": "eq", "fun": lambda x: np.sum(x) - 1})

# run optimization based on function to be minimized, starting with equal weights and based on respective bounds and constraints
opts = minimize(fun=min_func_sharpe, x0=init_weigths, method="SLSQP",
                bounds=bnds, constraints=cons)



但是如果使用手柄,您可以

const yourHref = await page.$eval('selector', anchor => anchor.getAttribute('href'));