标签: cheerio

用cheerio执行抓取的JavaScript

我有一个网页,其中有一些JS API不会改变dom,但返回一些数字.我想编写一个NodeJS应用程序来下载这些页面并在下载页面的上下文中执行这些功能.

我正在寻找cheerio页面抓取...但是虽然我看到用它来导航和操作DOM是多么容易,但我没有看到任何运行页面功能的访问权限.有可能吗?

相反,我应该在jsdom看?

谢谢

javascript node.js web-scraping cheerio

5
推荐指数
2
解决办法
4543
查看次数

获取cheerio页面的标题

我正在尝试用cheerio获取网址的标题标签.但是,我得到空字符串值.这是我的代码:

app.get('/scrape', function(req, res){

    url = 'http://nrabinowitz.github.io/pjscrape/';

    request(url, function(error, response, html){
        if(!error){
                        var $ = cheerio.load(html);

            var title, release, rating;
            var json = { title : "", release : "", rating : ""};

            $('title').filter(function(){
                //var data = $(this);
                var data = $(this);
                        title = data.children().first().text();            
                        release = data.children().last().children().text();

                json.title = title;
                json.release = release;
            })

            $('.star-box-giga-star').filter(function(){
                var data = $(this);
                rating = data.text();

                json.rating = rating;
            })
        }


        fs.writeFile('output.json', JSON.stringify(json, null, 4), function(err){

            console.log('File successfully written! - Check your project …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express cheerio

5
推荐指数
1
解决办法
8410
查看次数

使用node.js访问DOM

我想访问html文件并使用node.js获取id的元素,这是我的html文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Diagram </title>

<script>

    function generatePNG (oViewer) {
// some other code
            reader.onloadend = function() {
                base64data = reader.result;
                var image = document.createElement('img');
                image.setAttribute("id", "GraphImage");
                image.src = base64data;
                document.body.appendChild(image);
            }

        }, "image/png", oImageOptions);
        return sResult;

        var sResult = generatePNG (oEditor.viewer);

    });
</script>


</head>

<body >
    <div id="diagramContainer"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我想document.getElementById("GraphImage").src用node.js 来搞定 .我发现我可以使用cheeriojsdom访问DOM带有node.js,所以我尝试使用以下代码cheerio:

var cheerio = require('cheerio'),
    $ = cheerio.load('file.html');
Run Code Online (Sandbox Code Playgroud)

但我没有找到允许我image.src从html文件中获取的指令,就像这条指令:document.getElementById("GraphImage").src

javascript dom node.js jsdom cheerio

5
推荐指数
1
解决办法
1万
查看次数

Cheerio 错误,未定义不是函数 // 这是正确的方法吗?

尝试运行代码时,我不断收到错误$.find('.market_listing_item_name_block').each()- undefined 不是函数,指向 find。我认为 find 是cheerio中的一个函数?公平地说,我不确定我是否做对了,这是我的代码:

var cheerio = require('cheerio')
$ = cheerio.load('#searchResultsRows')
var url = 'http://steamcommunity.com/market/search?appid=730'
xhr.get(url).success(function(r){
    $.find(".market_listing_item_name_block").each(function(){
        var name = $(this).find(".market_listing_item_name").text();
        console.log(name)
    })
})
Run Code Online (Sandbox Code Playgroud)

xhr 是一个本质上类似于 AJAX 的对象。

我之前在 chrome 中的做法是:

var url = 'http://steamcommunity.com/market/search?appid=730'
var itemDiv = $("<div></div>")
$.get(url).success(function(r){
    d = $(r).find('#searchResultsRows')
    itemDiv.append(d)
})
Run Code Online (Sandbox Code Playgroud)

进而:

itemDiv.find(".market_listing_item_name_block").each(function(){
   var name = $(this).find(".market_listing_item_name").text();
   console.log(name) // would normally do other things, but for the purpose of this post, i'm just console logging the name
})
Run Code Online (Sandbox Code Playgroud)

我究竟如何才能在 node/cheerio 中重新创建那个 ^?我相信我显然错过了几步。非常感谢任何帮助,谢谢。

node.js cheerio

5
推荐指数
1
解决办法
3811
查看次数

如何从浏览器使用cheerio

我是 JavaScript 的新手,我很确定我在从 HTML 页面(由网络浏览器浏览)使用 JS 时遗漏了一些基本的东西。

我的目标是报废的照片链接从一个动态的网站使用cheerio并显示它们一个js的小工具(例如,使用lightslider),它看起来相当成功按照本教程以获得下面的脚本并运行它通过简单的NodeJS scrapt.js在bash终端:

var request = require('request');
var cheerio = require('cheerio');
request('https://outbox.eait.uq.edu.au/uqczhan2/Photos/', function (error, respo
  if (!error && response.statusCode == 200) {
    console.log(html);
  }
});
Run Code Online (Sandbox Code Playgroud)

但是现在我无法在通用网络浏览器中运行此脚本(按 f12 -> 控制台),因为第一个语法后显示错误:

>var request = require('request');
VM85:1 Uncaught ReferenceError: require is not defined
    at <anonymous>:1:15
Run Code Online (Sandbox Code Playgroud)

我知道在使用之前需要加载一些 JavaScript 模块,例如 d3.js。我需要运行:

<script src="https://d3js.org/d3.v4.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

使用所有 d3 功能。我应该如何实现让我在 Web 浏览器中使用cheerio 的相同功能?

javascript node.js cheerio

5
推荐指数
1
解决办法
4377
查看次数

Cheerio 无需类即可获取图像 Src

尝试使用 Cheerio 拉取 img 源,但 img 没有类。看起来像

<div class="container_c89a5 lazyLoadContainer_b1038">
<img height="80" src="https://stuff.com" srcset="https://stuff.com" width="80">
</div>
Run Code Online (Sandbox Code Playgroud)

我试过以几种不同的方式选择图像源,但没有运气。

var $ = cheerio.load(html);
    $('div.item_54fdd').each(function(i, element) {
        var a = $(this);
        var title = a.find('.title_9ddaf').text(); //works great
        var image = a.find('div.container_c89a5').first('img').attr('src');  //no luck
        var image = a.find('div.container_c89a5 > img').attr('src');  //no luck
Run Code Online (Sandbox Code Playgroud)

jquery node.js cheerio

5
推荐指数
1
解决办法
1万
查看次数

Iterate through an html table using Cheerio

I have to iterate through a table and create a json object containing info in this way:

var obj = {
    vaccine: "...",
    year: ...,
    country: "...",
    coverage: ...
} 
Run Code Online (Sandbox Code Playgroud)

The table is:

<table class="ts">
    <tr>
        <td class="statheadings" colspan="100%" align="center">
            <h1 class="statistics">Coverage time series for Italy&nbsp;&nbsp;(ITA)</h1>
        </td>
    </tr>
    <tr>
        <td align="center" colspan="100%"> <font color="red">
            Last updated 06-Sep-2017 (data as of 05-Sep-2017)<br />Next overall update 2018<br /></font>
        </td>
    </tr>
    <tr>
        <td colspan="100%" >
            <hr />
        </td>
    </tr>

    <tr>
        <th colspan="4" align="left">Vacciness</th> …
Run Code Online (Sandbox Code Playgroud)

json node.js web-scraping cheerio

5
推荐指数
1
解决办法
3183
查看次数

Alexa AWS 错误:任务在 3.00 秒后超时

我正在尝试编写一个 Alexa 技能,它使用 request.js 和cheerio.js 对数据进行一些网络抓取。

当我在本地运行它时,它按预期工作正常,但在 AWS lambda 实例上,我不断收到超时错误,因为它需要超过 3 秒才能完成运行。错误日志是这样的:

    START RequestId: 57f84024-6ef5-11e8-b49f-37c904d87c93 Version: $LATEST
2018-06-13T10:34:26.850Z    57f84024-6ef5-11e8-b49f-37c904d87c93    NEW SESSION
2018-06-13T10:34:26.851Z    57f84024-6ef5-11e8-b49f-37c904d87c93    INTENT REQUEST
2018-06-13T10:34:26.851Z    57f84024-6ef5-11e8-b49f-37c904d87c93    Sid started
2018-06-13T10:34:26.851Z    57f84024-6ef5-11e8-b49f-37c904d87c93    INTENT FOR SID HAS STARTED
2018-06-13T10:34:26.851Z    57f84024-6ef5-11e8-b49f-37c904d87c93    https://en.wikipedia.org/wiki/Chloroform
2018-06-13T10:34:26.950Z    57f84024-6ef5-11e8-b49f-37c904d87c93    SESSION END REQUEST
2018-06-13T10:34:27.731Z    57f84024-6ef5-11e8-b49f-37c904d87c93    REQUEST MADE
2018-06-13T10:34:27.731Z    57f84024-6ef5-11e8-b49f-37c904d87c93    trying to use cheerio to load page now
END RequestId: 57f84024-6ef5-11e8-b49f-37c904d87c93
REPORT RequestId: 57f84024-6ef5-11e8-b49f-37c904d87c93  Duration: 3003.16 ms    Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 48 MB …
Run Code Online (Sandbox Code Playgroud)

request node.js cheerio aws-lambda alexa-skill

5
推荐指数
2
解决办法
8221
查看次数

使用谷歌云功能抓取它完成状态代码:304

我正在尝试谷歌云功能,它可以工作,但以 304 的状态代码结束,不知道是什么原因。下面是代码,

//gcloud beta functions deploy scrapeGitCollection --trigger-http
var cheerio = require('cheerio');
var request = require('request');

function getDateTime() {

    var date = new Date();

    var hour = date.getHours();
    hour = (hour < 10 ? "0" : "") + hour;

    var min  = date.getMinutes();
    min = (min < 10 ? "0" : "") + min;

    var sec  = date.getSeconds();
    sec = (sec < 10 ? "0" : "") + sec;

    var year = date.getFullYear();

    var month = date.getMonth() + 1; …
Run Code Online (Sandbox Code Playgroud)

node.js cheerio google-cloud-functions

5
推荐指数
1
解决办法
3339
查看次数

用 Cheerio 替换 HTML 节点

我正在使用Cheerio JS来简化一些古老的 HTML 代码并将其转换为 HTML5。除其他事项外,我正在替换一些如下所示的标记重引号:

要替换的节点:

<div style="margin:20px; margin-top:5px; ">
    <div class="smallfont" style="margin-bottom:2px">Quote:</div>
    <table cellpadding="6" cellspacing="0" border="0" width="100%">
        <tbody>
            <tr>
                <td class="alt2" style="border:1px solid #999">
                    <div>
                        Originally Posted by <strong>Username</strong>
                    </div>
                    <div style="font-style:italic">Lorem ipsum dolor sit amet</div>
                </td>
            </tr>
        </tbody>
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

转换后的输出应该是这样的:

<blockquote>Lorem ipsum dolor sit amet</blockquote>
Run Code Online (Sandbox Code Playgroud)

这是我此时使用的代码当前代码:

$(`table[id^='post']`).each( (i, el) => {
    // Get the post
    let postBody = $(el).find(`div[id^='post_message_']`).html().trim();

    // Replace quotes with blockquotes
    cheerio.load(postBody)('div[style^="margin:20px; margin-top:5px; "]').each( (i, el) => {
        if ($(el).html().trim().startsWith('<div class="smallfont" style="margin-bottom:2px">Quote')) …
Run Code Online (Sandbox Code Playgroud)

javascript dom node.js cheerio

5
推荐指数
2
解决办法
4618
查看次数