Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在<;帆布>;元素使用python还是javascript?_Javascript_Python_Python 3.x_Web Scraping_Chart.js - Fatal编程技术网

如何在<;帆布>;元素使用python还是javascript?

如何在<;帆布>;元素使用python还是javascript?,javascript,python,python-3.x,web-scraping,chart.js,Javascript,Python,Python 3.x,Web Scraping,Chart.js,我想从网站上抓取数据,比如在元素中呈现交互式图表,并且不将任何数据显示为可抓取的HTML元素。 检查HTML时,页面似乎要使用 虽然python中的帮助是首选,但如果我真的需要使用一些javascript,也可以 另外,我希望避免使用需要额外文件的方法,例如phantomjs,但是,如果这是唯一的方法,请慷慨地分享它。解决这个问题的一个方法是在页面源代码第1050行附近查看页面的,这实际上是图表初始化的地方。在图表的初始化过程中有一个循环模式,其中逐个查询画布元素以获取其上下文,然后是提供图表标

我想从网站上抓取数据,比如在
元素中呈现交互式图表,并且不将任何数据显示为可抓取的HTML元素。 检查HTML时,页面似乎要使用

虽然python中的帮助是首选,但如果我真的需要使用一些javascript,也可以


另外,我希望避免使用需要额外文件的方法,例如phantomjs,但是,如果这是唯一的方法,请慷慨地分享它。

解决这个问题的一个方法是在页面源代码第1050行附近查看页面的
,这实际上是图表初始化的地方。在图表的初始化过程中有一个循环模式,其中逐个查询画布元素以获取其上下文,然后是提供图表标签和统计信息的变量

此解决方案包括使用node.js,至少是包含以下模块的最新版本:

  • 用于查询DOM中的元素
  • 用于发送http请求以获取页面源
  • 以获取我们希望刮取的脚本的javascript对象树表示
下面是代码和源代码:

const cheerio = require('cheerio');

const axios = require('axios');

const { parse, each, find } = require('abstract-syntax-tree');

async function main() {

    // get the page source
    const { data } = await axios.get(
        'https://stats.warbrokers.io/players/i/5d2ead35d142affb05757778'
    );

    // load the page source with cheerio to query the elements
    const $ = cheerio.load(data);

    // get the script tag that contains the string 'Chart.defaults'
    const contents = $('script')
        .toArray()
        .map(script => $(script).html())
        .find(contents => contents.includes('Chart.defaults'));

    // convert the script content to an AST
    const ast = parse(contents);

    // we'll put all declarations in this object
    const declarations = {};

    // current key
    let key = null;

    // iterate over all variable declarations inside a script
    each(ast, 'VariableDeclaration', node => {

        // iterate over possible declarations, e.g. comma separated
        node.declarations.forEach(item => {

            // let's get the key to contain the values of the statistics and their labels
            // we'll use the ID of the canvas itself in this case..
            if(item.id.name === 'ctx') { // is this a canvas context variable?
                // get the only string literal that is not '2d'
                const literal = find(item, 'Literal').find(v => v.value !== '2d');
                if(literal) { // do we have non- '2d' string literals?
                    // then assign it as the current key
                    key = literal.value;
                }
            }

            // ensure that the variable we're getting is an array expression
            if(key && item.init && item.init.type === 'ArrayExpression') {

                // get the array expression
                const array = item.init.elements.map(v => v.value);

                // did we get the values from the statistics?
                if(declarations[key]) {

                    // zip the objects to associate keys and values properly
                    const result = {};
                    for(let index = 0; index < array.length; index++) {
                        result[array[index]] = declarations[key][index];
                    }
                    declarations[key] = result;

                    // let's make the key null again to avoid getting
                    // unnecessary array expression
                    key = null;

                } else {
                    // store the values
                    declarations[key] = array;
                }
            }

        });

    });

    // logging it here, it's up to you how you deal with the data itself
    console.log(declarations);

}

main();
const cheerio=require('cheerio');
const axios=require('axios');
const{parse,each,find}=require('abstract-syntax-tree');
异步函数main(){
//获取页面源代码
const{data}=wait axios.get(
'https://stats.warbrokers.io/players/i/5d2ead35d142affb05757778'
);
//使用ChereIO加载页面源以查询元素
const$=cheerio.load(数据);
//获取包含字符串“Chart.defaults”的脚本标记
常量内容=$(“脚本”)
.toArray()
.map(脚本=>$(脚本).html())
.find(contents=>contents.includes('Chart.defaults');
//将脚本内容转换为AST
const ast=解析(内容);
//我们将把所有声明放在这个对象中
常量声明={};
//当前键
让key=null;
//迭代脚本中的所有变量声明
每个(ast,'VariableDeclaration',节点=>{
//迭代可能的声明,例如逗号分隔的
node.declarations.forEach(项=>{
//让我们获取包含统计信息值及其标签的键
//在本例中,我们将使用画布本身的ID。。
如果(item.id.name==='ctx'){//这是画布上下文变量吗?
//获取唯一不是“2d”的字符串文字
常量literal=find(项“literal”).find(v=>v.value!==2d”);
如果(literal){//我们是否有非“2d”字符串文本?
//然后将其指定为当前密钥
key=literal.value;
}
}
//确保我们得到的变量是数组表达式
if(key&&item.init&&item.init.type==='ArrayExpression'){
//获取数组表达式
常量数组=item.init.elements.map(v=>v.value);
//我们从统计数据中得到了值吗?
if(声明[键]){
//压缩对象以正确关联键和值
const result={};
for(让index=0;index
对于python,您可以使用selenium。您可以共享页面的url吗?@IainShelvington我不知道如何使用selenium从画布上刮取数据。我是一个刮网高手;;;您不能刮去画布,因为它类似于图像,所以您需要使用一些软件进行图像识别,但您可以在页面上的画布中找到所有数据,例如在这个标记//div[@class='playerStatPage']/下面的兄弟::script或in-image元素,例如this//div[@id='ribbons-sm']/div[@class='ribbon-wrapper'],您不需要javascript