【问题标题】:How do I curl a page that needs first clicking a consent button?如何卷曲需要首先单击同意按钮的页面?
【发布时间】:2021-01-27 01:55:39
【问题描述】:

我喜欢像 this 这样的 URL 卷曲。 (如果您没有该网站的 cookie,则该 URL 需要在欧盟国家/地区获得同意。)

我拼凑了一个执行此操作的 puppeteer 脚本,但在我看来它非常沉重且脆弱。有更好的解决方案吗?

#!/usr/bin/env node

const url = process.argv[2];
const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()


  await page.goto(url)

  await page.waitForSelector('.con-wizard > .wizard-body > #consent-text > .content-list > .list-item:nth-child(1)')
  await page.click('.con-wizard > .wizard-body > #consent-text > .content-list > .list-item:nth-child(1)')

  await page.waitForSelector('.con-wizard > .wizard-body > .actions > .consent-form > .primary')
  await page.click('.con-wizard > .wizard-body > .actions > .consent-form > .primary')


    const timeout = ((process.env.cfTimeout) || 20) * 1000

    await page.waitFor(timeout);
    const html = await page.content();
    console.log(html);

  await browser.close()
})()

【问题讨论】:

    标签: curl web-scraping cookies puppeteer wget


    【解决方案1】:

    通过在 Chrome 的网络窗格中监视请求,我成功地使用普通 curl 做到了这一点:

    function techcrunch-curl() {
        local url="${1:?}"
        local con="$(curl -o /dev/null -w %{url_effective} $url)"
        if [[ "$con" =~ 'https://consent\.yahoo\.com/v2/collectConsent\?sessionId=(.*)' ]] ; then
            local sid="${match[1]}"
    
            curl -o /dev/stdout --fail --location --cookie-jar =() 'https://consent.yahoo.com/v2/collectConsent?sessionId='$sid \
                -H 'Connection: keep-alive' \
                -H 'Pragma: no-cache' \
                -H 'Cache-Control: no-cache' \
                -H 'Origin: https://consent.yahoo.com' \
                -H 'Upgrade-Insecure-Requests: 1' \
                -H 'DNT: 1' \
                -H 'Content-Type: application/x-www-form-urlencoded' \
                -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36' \
                -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
                -H 'Sec-Fetch-Site: same-origin' \
                -H 'Sec-Fetch-Mode: navigate' \
                -H 'Sec-Fetch-User: ?1' \
                -H 'Sec-Fetch-Dest: document' \
                -H 'Referer: https://consent.yahoo.com/v2/collectConsent?sessionId='$sid \
                -H 'Accept-Language: en-US,en;q=0.9' \
                --data-raw 'sessionId='$sid'&originalDoneUrl='"$(<<<$url url-encode.py)"'&namespace=techcrunch&agree=agree&agree=agree' \
                --compressed
        else
            curl $url
        fi
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多