【问题标题】:Scraping the data of the advent of code in jsjs代码的数据采集问世
【发布时间】:2022-12-10 23:01:51
【问题描述】:

我正在尝试废弃代码出现的输入。为此,我使用此代码:

import got from 'got';


(async () =>{
    const url = 'https://adventofcode.com/2022/day/1/input'
    try {
        const response = await got.get(url);
        console.log(response.body)
        console.log(typeof(response.body))
    } catch (error) {
        console.log(error)
    }
})();

但是我得到了错误:HTTPError: Response code 400 (Bad Request)

它与其他 url 一起工作,我在我的 Web 浏览器中看到数据。 所以我不明白这是什么问题...

【问题讨论】:

  • 他们使用 cookie 来确保每个用户都能获得他们的自定义输入。您不能只做一个简单的获取请求来接收您的输入。

标签: javascript web-scraping


【解决方案1】:

此站点需要在标头中使用 cookie 才能向您发送数据

添加cookie解决问题

import got from 'got';


(async () =>{
    const url = 'https://adventofcode.com/2022/day/1/input'
    try {
        const response = await got.get(url, {
          headers: {
            "cookie": /* your cookie here */
          }
        });
        console.log(response.body)
        console.log(typeof(response.body))
    } catch (error) {
        console.log(error)
    }
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2012-07-15
    • 2019-05-03
    • 2012-02-12
    相关资源
    最近更新 更多