从Nightmare.js

时间:2016-09-25 20:30:51

标签: javascript node.js nightmare

我目前正在使用cheerionightmare进行一些抓取工作。我之所以使用这两者而不仅仅是cheerio的原因是因为我必须操纵网站才能找到我想要抓取的部分,而且我发现噩梦非常擅长做这些脚本。

所以,现在我使用nightmare来获取显示我需要的信息的部分。之后,在evaluate()我试图以某种方式返回当前html然后将其传递给cheerio以进行刮擦。问题是我不知道如何从document对象中检索html。 document是否有属性可以返回完整的正文?

以下是我要做的事情:

var Nightmare = require('nightmare');
var nightmare = Nightmare({show:true})
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app     = express();

var urlWeb = "url";
var selectCity = "#ddl_city"

nightmare
.goto(urlWeb)
.wait(selectCity)
.select('#ddl_city', '19')
.wait(6000)
.select('#ddl_theater', '12')
.wait(1000)
.click('#btn_enter')
.wait('#aspnetForm')
.evaluate(function(){

    //here is where I want to return the html body
    return document.html;


})
.then(function(body){
//loading html body to cheerio
    var $ = cheerio.load(body);
    console.log(body);
})

1 个答案:

答案 0 :(得分:10)

有了这个:

CREATE TABLE Customer (
    customerid varchar(10) NOT NULL, 
    FirstName varchar(20) NOT NULL, 
    LastName varchar(20) NOT NULL, 
    StreetAddress varchar (30) NOT NULL, 
    City varchar (20)NOT NULL, 
    State varchar (20) NOT NULL, 
    Zip int(5) NOT NULL, 
    Hphone int (10) NOT NULL, 
    Mphone int (10) NOT NULL, 
    Ophone int (10) NOT NULL, 
    PRIMARY KEY (customerid));

CREATE TABLE OrderTable (
    donutorderid varchar(10) NOT NULL, 
    customerid varchar (10) NOT NULL,
    oderdate datetime (6) NOT NULL,
    PRIMARY KEY (donutorderid)
);

CREATE TABLE Donut (
    donutid varchar(10),
    donutname varchar(20),
    description varchar(30),
    unitprice numeric,
    PRIMARY KEY (donutid)
);

CREATE TABLE OrderLine (
    donutorderid varchar (30),
    donutid varchar (30),
    qty int (10),
    PRIMARY KEY (donutorderid,donutid)
);

ALTER TABLE OrderTable ADD INDEX checks (customerid), 
ADD CONSTRAINT checks FOREIGN KEY (customerid) REFERENCES Customer (customerid);
ALTER TABLE OrderLine ADD INDEX has (donutorderid), 
ADD CONSTRAINT has FOREIGN KEY (donutorderid) REFERENCES OrderTable (donutorderid);
ALTER TABLE OrderLine ADD INDEX available_in (donutid), 
ADD CONSTRAINT available_in FOREIGN KEY (donutid) REFERENCES Donut (donutid);

create view CustInfo
    AS Select customerid, concat(FirstName,LastName) as FullName from customer;

insert into customer
    (customerid, FirstName, LastName, StreetAddress, 
    City, State, Zip, Hphone, Mphone, Ophone) 
values ('123','John','Doe', 'one hoover lane', 'las vegas', 
        'nevada', 89104, 702555122, 702441111, 702332222);