所以我正在抓取一个网站,我希望它向innerHTML 显示数据,但我在让它工作时遇到了困难。我正在尝试使用
document.getElementById('results').innerHTML = searchJobs('');
但它告诉我文档未定义,对不起,这是个菜鸟问题,但问起来比在同一件事上停留 45 分钟更容易。
这是代码:
function searchJobs(i) {
const url = axios.get('actualurl')
return fetch(`${url}${i}`)
.then(response => response.text())
.then(res => {
const jobs = [];
const jobsBody = [];
const $ = cheerio.load(res.data);
$('.result').each((index, element) => {
const title = $(element).children('.title').text();
const linkToJob = $(element).children('.title').children('a').attr('href')
const body = $(element).children('.summary').text();
jobs[index] = { title, linkToJob, body };
});
return jobs;
});
}
document.getElementById('results').innerHTML = searchJobs('');
module.exports = searchJobs;
我可以将抓取工具发送到 console.log 就好了。
索引路线:
const express = require('express');
const router = express.Router();
const scraper = require('../scrapers/scrapers');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
应用程序.js:
const express = require('express'),
path = require('path'),
cookieParser = require('cookie-parser'),
logger = require('morgan'),
puppeteer = require('puppeteer'),
axios = require('axios'),
cheerio = require('cheerio');
es6Renderer = require('express-es6-template-engine');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.engine('html', es6Renderer);
app.set('views', './views');
app.set('view engine', 'html');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
一只萌萌小番薯
相关分类