包含使用Node + HTML的脚本

时间:2014-09-17 04:45:54

标签: javascript jquery node.js

我编写了一个节点脚本来擦除urbandictionary.com中的内容。它输出一个10个单词的数组,描述它所指向的任何页面。

现在我想将它附加到一个HTML页面,让你在输入框中写下你的名字,然后看到描述你的词。当我从控制台运行脚本时,它工作得很好,但是现在我已经将它作为HTML文件中的脚本包含在内,但它不起作用。

节点脚本似乎在require request cheeriolodash&amp; <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>urbancloud</title> <link rel="stylesheet" type="text/css" href="jqcloud.css" /> <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script type="text/javascript" src="jqcloud-1.0.4.min.js"></script> <script type="text/javascript" src="scrape.js"></script> </head> <body> <input type="text"> <button id="nameRefresh">Click</button> <div id="cloud" style="width: 550px; height: 350px";></div> </body> </html>

我尝试将它们包含在HTML头中,但这似乎也没有。

所以我的问题是:如何让Node脚本访问它所需的资源(lodash,request,cheerio)并仍然从HTML文件访问它?我应该使用Node吗?我考虑过AJAX,但是它上面的所有介绍教程都表示你只能从你所在的同一个域请求。

的index.html

var request = require('request');
var cheerio = require('cheerio');
var _ = require('lodash-node');
var cloudWords = [];
var i;
var url = 'http://www.urbandictionary.com/define.php?term=James';

request(url, function(err, resp, body) {
    var counts = {};
    if (err)
        throw err;
    $ = cheerio.load(body); //setup the DOM
    var descrip = $('.meaning').text() //.meaning is the user-submitted description on urbandictionary.com

    // \/ \/ \/ parse the string, put the words into an array \/ \/ \/
    descrip = descrip.replace(/[\r\n\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"").toLowerCase().split(" ").sort(); /
    descrip = _.uniq(descrip);
    descrip = _.without(descrip, 'very', 'which', 'well', 'was', 'who', 'with', 'would', 'your', 'un', 'i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it', 'to', 'their', 'them', 'la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www',"a","able","about","across","after","all","almost","also","am","among","an","and","any","are","as","at","be","because","been","but","by","can","cannot","could","dear","did","do","does","either","else","ever","every","for","from","get","got","had","has","have","he","her","hers","him","his","how","however","i","if","in","into","is","it","its","just","least","let","like","likely","may","me","might","most","must","my","neither","no","nor","not","of","off","often","on","only","or","other","our","own","rather","said","say","says","she","should","since","so","some","than","that","the","their","them","then","there","these","they","this","tis","to","too","twas","us","wants","was","we","were","what","when","where","which","while","who","whom","why","will","with","would","yet","you","your","ain't","aren't","can't","could've","couldn't","didn't","doesn't","don't","hasn't","he'd","he'll","he's","how'd","how'll","how's","i'd","i'll","i'm","i've","isn't","it's","might've","mightn't","must've","mustn't","shan't","she'd","she'll","she's","should've","shouldn't","that'll","that's","there's","they'd","they'll","they're","they've","wasn't","we'd","we'll","we're","weren't","what'd","what's","when'd","when'll","when's","where'd","where'll","where's","who'd","who'll","who's","why'd","why'll","why's","won't","would've","wouldn't","you'd","you'll","you're","you've");

    //take 10 random words to put into a text cloud
    for (var i = 0; i < 10; i++) {
        cloudWords[i] = descrip[Math.floor(Math.random()*descrip.length)];
    }
console.log(cloudWords);

scrape.js

{{1}}

0 个答案:

没有答案