使用Apify进行网页抓取

时间:2020-02-25 04:29:50

标签: javascript web-scraping screen-scraping puppeteer apify

我正在尝试从https://en.wikipedia.org/wiki/List_of_hedge_funds抓取网址

具体来说,我正在尝试使用Apify抓取该页面并从HTML中存在的定位标记返回URL列表。在我的控制台中,我希望在目标页面上的href属性中看到一个或多个锚点标签的myValue属性的值。我还希望在名为title的属性中看到页面标题。相反,我只是看到以下URL属性及其值。

enter image description here

我的Apify演员使用Puppeteer平台。所以我正在使用pageFunction similar to the way Puppeteer uses it

下面是在我运行Apify UI之前的屏幕截图。

enter image description here

页面功能
function pageFunction( context ) {
    // called on every page the crawler visits, use it to extract data from it
    var $ = context.jQuery;
    var result = {
        title: $('.wikitable').text,
        myValue: $('a[href]').text,
    };
    return result;
} 

我在做什么错了?

2 个答案:

答案 0 :(得分:0)

您的代码中有错字,text是一个函数,因此您需要添加括号:

var result = {
    title: $('.wikitable').text(),
    myValue: $('a[href]').text(),
};

但是请注意,无论如何这可能不会实现您期望的效果-它会返回所有匹配元素的文本。您可能需要使用jQuery's each()函数(https://api.jquery.com/jquery.each/)来迭代找到的元素,将其中的一些值压入数组,然后从页面函数返回该数组。

答案 1 :(得分:0)

该页面似乎由JavaScript加载,因此实际上我必须使用异步代码。