Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

简单的Node网页内容抓取实战 #5

Open
Vexth opened this issue Dec 28, 2017 · 0 comments
Open

简单的Node网页内容抓取实战 #5

Vexth opened this issue Dec 28, 2017 · 0 comments

Comments

@Vexth
Copy link
Owner

Vexth commented Dec 28, 2017

Nodejs写网页内容抓取代码,很简单!!!只要3个包就可以解决这个问题。
Nodejs自带的fshttp或者https,还有一个cheerio
安装cheerioyarn add cheerio
实现代码:

const http = require('http');
const fs = require('fs');
const cheerio = require('cheerio');

const URL = `http://www.qdfuns.com/`;

let id = 0;
let cls = 'h2.media-heading a:last-child';

function startRequest(ID) {
    id++;
    let url = URL + `notes/id/all:all:all:82dec52c2ad6cd55dc2a84ee5cfdc713/page/${id}.html`;
    return http.get(url, res => {
        res.setEncoding('utf8');
        let rawData = '';
        let Arr = [];
        res.on('data', chunk => { rawData += chunk; });
        res.on('end', () => {
            try {
                let $ = cheerio.load(rawData);
                
                $(cls).each((i, data) => {
                    Arr.push({
                        title: data.children[0].data,
                        url : data.children[0].parent.attribs.href
                    })
                    // console.log(data.children[0].data);
                    // console.log(data.children[0].parent.attribs.href);
                })

                fs.writeFile(__dirname + '/data/article.json', JSON.stringify({
                    status: 0,
                    data: Arr
                }), (err) => {
                    if (err) throw err;
                    console.log('文章列表写入完成');
                });

            } catch (e) {
                console.error(e.message);
            }
        });

        if (id < ID) {
            return startRequest(ID);
        }
    }).on('error', e => {
        console.error(`错误: ${e.message}`);
    });
}

startRequest(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant