使用JavaScript控制台从网页获取数据的问题

时间:2018-06-15 21:01:18

标签: javascript html console webpage

我在通过此控制台脚本从网页抓取数据时遇到问题。 我试图从HTML的这一部分得到15:

<td class="credit">15</td>

但这总是失败......

try {
    resolve(Number(data.match(/credit">[\d,,]*/g)[0].replace('credit">', '').replace(/,/g, '')));
} catch(e) {
    resolve(1);
};

请告诉我我做错了什么,谢谢。

完整代码:

const userid = 44600777;
const groupurl = `https://www.example.com/users/profile/playergroups-json? userId=${userid}`;
let Total = 0;

function GetFunds(id) {
    let url = `https://www.example.com/my/groupadmin.aspx?gid=${id}#nav-revenue`;
    return new Promise((resolve, reject) => {
        try {
            fetch(url, {
                credentials: 'same-origin'
            }).then((response) => response.text()).then((data) => {
                try {
                    resolve(Number(data.match(/credit">[\d,,]*/g)[0].replace('credit">', '').replace(/,/g, '')));
                } catch(e) {
                    resolve(1);
                };
            });
        } catch(e) {
            resolve(2);
        };
    });
};

fetch(groupurl).then((response) => response.json()).then(async (data) => {
    for(let group of data.Groups) {
        let Amount = await GetFunds(group.Id);
        Total += Amount;
    };
    console.log('Total: ' + new Intl.NumberFormat().format(Total) + ' ($' + Total * 0.0045 + ')');
});

0 个答案:

没有答案