简体   繁体   English

如何在Phantomjs中使用jQuery选择html元素?

[英]How to select html elements using jQuery in Phantomjs?

I am going to scrape some contents from a website that use javascript to load dynamic content. 我将从网站上抓取一些使用javascript加载动态内容的内容。 Before, I have used request and cheerio to scrape and they worked just fine. 以前,我曾使用requestcheerio进行抓取,并且它们工作得很好。 But I just find out that request and cheerio cannot scrape dynamic content. 但是我只是发现请求快乐无法抓取动态内容。 After do a research, I found phantomjs that can get all the content after the page has loaded. 经过研究,我发现phantomjs可以在页面加载后获取所有内容。 I have a problem with it now, I cannot use jQuery selector like I used to use in cheerio . 我现在有一个问题,我不能像过去在cheerio中那样使用jQuery选择器 This is my sample code but the selector is return nothing. 这是我的示例代码,但选择器未返回任何内容。

var page = require('webpage').create();
var url = 'http://angkorauto.com/vehicle';
page.open(url, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        window.setTimeout(function () {
            // console.log(page.content);
            page.includeJs('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js', function(){

                page.evaluate(function(){
                    console.log($('.divTitle').find('a').attr('href'));
                });
            });

            phantom.exit();
        }, 1500);
    }
});

Could you help me with this problem? 您能帮我解决这个问题吗? I really get stuck now. 我现在真的被卡住了。

Thanks for ur time to help. 多谢您的协助。

The website you want to scrape has jQuery already (like many other websites) so you don't have load it again. 您要抓取的网站已经具有jQuery(与许多其他网站一样),因此您无需再次加载它。

This works fine: 这工作正常:

var page = require('webpage').create();
var url = 'http://angkorauto.com/vehicle';
page.open(url, function(status) {

    var href = page.evaluate(function(){
        return jQuery('.divTitle').find('a').attr('href');
    });

    console.log(href);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM