Cheerio 网页抓取不返回抓取的链接

时间:2021-06-15 06:57:09

标签: javascript node.js cheerio

我正在尝试获取 google 搜索的链接,并使用 node js 和cheerio 来抓取这些链接。带有 jQ​​uery 的 DOM 选择器在浏览器控制台中运行良好,但是当我运行我的代码时,它输出一个空数组。我正在使用以下代码

const cheerio = require("cheerio");
const axios = require("axios").default;
(async () => {
  const getData = async (url) => {
    const { data } = await axios(url);
    const $ = cheerio.load(data);

    
     const links = Array.from($('div[class="yuRUbf"] >a')).map((a) => a.href);
    console.log(links);
  };
  getData(
    "https://www.google.com/search?q=Let+You+Love+Me+by+Rita+Ora&sxsrf=ALeKk02Hp5Segi8ShvyrREw3NLZ6p7_BKw:1622526254457&ei=Lsm1YPSzG9WX1fAPvdqTgAg&sa=N&ved=2ahUKEwj0gqSo3fXwAhXVSxUIHT3tBIAQ8tMDegQIARA7&biw=1517&bih=694"
  );
})();


1 个答案:

答案 0 :(得分:0)

我调试了您的代码并发现了以下问题

  1. 您没有传递未获取所需数据的用户代理标头,因此我们不得不将用户代理模拟为“浏览器”,以便选择器类可用。
  2. 虽然采用锚标记语法的 href 似乎是错误的

添加我测试过的重新访问的代码工作正常。

const cheerio = require("cheerio");
const axios = require("axios");
(async () => {
    const getData = async (url) => {
        const { data } = await axios.get(url, {
            headers: {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36",
            },
        });
        const $ = cheerio.load(data);
        const links = Array.from($('div[class="yuRUbf"] >a')).map((a) => {
            return $(a).attr('href')
        });
        console.log(links);
    };
    getData(
        "https://www.google.com/search?q=Let+You+Love+Me+by+Rita+Ora&sxsrf=ALeKk02Hp5Segi8ShvyrREw3NLZ6p7_BKw:1622526254457&ei=Lsm1YPSzG9WX1fAPvdqTgAg&sa=N&ved=2ahUKEwj0gqSo3fXwAhXVSxUIHT3tBIAQ8tMDegQIARA7&biw=1517&bih=694"
    );
})();