【问题标题】:Requests suddenly not working despite no apparent change from scraping source or code尽管抓取源代码或代码没有明显变化,但请求突然不起作用
【发布时间】:2019-08-18 14:31:15
【问题描述】:

我目前正在尝试更好地使用 JS 抓取并使用 request 和 Cheerio。大约两周前,我得到了一个基本的亚马逊刮擦工作,但今天早上当我加载我的文件时,它不再工作了。我确保在节点上安装了 Cheerio 和 Request,并尝试从 wikipedia 获取请求,并且效果很好。在亚马逊上我的原始源代码不再有效。他们的网页上似乎没有任何变化,所以我不知道为什么我的目标都没有工作。

const request = require('request');
const cheerio = require('cheerio');

request(`http://amazon.com/dp/B07R7DY911`, (error,response,html) =>{
    if (!error && response.statusCode ==200) {
        const $ = cheerio.load(html);
        const productTitle = $("#productTitle").html()
        const price = $("#priceblock_ourprice").text();
        const rating = $('#centerCol #acrPopover').text().replace(/\s\s+/g, '');
        const numReviews = $('#centerCol #acrCustomerReviewText').text().replace(/\s\s+/g, '');
        const prodImg = $('#landingImage').attr('data-old-hires');

        console.log(productTitle);
        console.log(price);
        console.log(rating);
        console.log(numReviews);
        console.log(prodImg)
    } else {
        console.log(error);
    }
})

一些玩弄,我得到了 null 和 undefined ,而我以前根本没有。

帮我堆栈溢出。你是我唯一的希望!

更新:

将代码切换到 axios。现在好多了。

app.get("/",(req,res)=>{
    axios.get(`${link}`)
      .then((response)=> {
        const html = response.data;
        const $ = cheerio.load(html);
    
        const productName = $("#productTitle").html().replace(/\s\s+/g, '');
        const amznPrice = $("#priceblock_ourprice").text();
        const rating = $('#centerCol #acrPopover').text().replace(/\s\s+/g, '');
        const numReviews = $('#centerCol #acrCustomerReviewText').text().replace(/\s\s+/g, '');
        const prodImg = $('#landingImage').attr('data-old-hires');
        res.render("home", {
            productTitle: productName,
            price:amznPrice,
            prod_Img:prodImg,
            azLink:links,
            });
    });
     

});

【问题讨论】:

  • 您确认您正在使用的选择器存在于您正在获取的 HTML 中吗?您可能不会获得相同的 HTML - 您可能会在节点应用程序中获得“机器人验证”页面(同时在常规浏览器中仍然获得常规 HTML)(参见顶部相关的question)。我会将收到的 HTML 保存在一个文件中,在本地浏览器中加载它,然后使用开发者工具找到相关的选择器并检查它们是否确实存在。

标签: node.js web-scraping request cheerio


【解决方案1】:

您似乎得到了request() 库无法理解的格式的压缩输出。如果您在request() 调用中添加gzip: true 选项,则代码开始为我工作。

const request = require('request');
const cheerio = require('cheerio');

request({url: 'http://amazon.com/dp/B07R7DY911', gzip: true}, (error,response,html) => {
    if (!error && response.statusCode == 200) {
        const $ = cheerio.load(html);
        const productTitle = $("#productTitle").html()
        const price = $("#priceblock_ourprice").text();
        const rating = $('#centerCol #acrPopover').text().replace(/\s\s+/g, '');
        const numReviews = $('#centerCol #acrCustomerReviewText').text().replace(/\s\s+/g, '');
        const prodImg = $('#landingImage').attr('data-old-hires');

        console.log("productTitle", productTitle);
        console.log("price", price);
        console.log("rating", rating);
        console.log("numReviews", numReviews);
        console.log("prodImg", prodImg)
    } else {
        console.log(error);
    }
});

【讨论】:

  • 这看起来是补救措施。奇怪的是,这在之前没有启用 gzip 的情况下工作。不知道为什么。我想我以后要根据要求来 RTFM。谢谢 jfriend00!
  • @VI11age_ID10T - 我的理论是亚马逊将其默认页面压缩更改为 request() 不支持的内容,因此您现在必须在请求中请求 GZIP。
  • 在请求地狱的时间比我关心的要长很多之后,我切换到了 axios
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多