TypeScript 程序代码示例

62 阅读1分钟

huake_00219_.jpg 基本的爬虫程序的示例:

import * as request from 'request';

// 信息
const proxyHost = '';
const proxyPort = ;

// 网站的 URL
const url = '';

// 使用 request 库发起请求
request({
    url, 
    method: 'GET', 
    proxy: {
        host: proxyHost,
        port: proxyPort
    }
}, (error, response, body) => {
    if (error) {
        console.log('Error:', error);
    } else {
        console.log('Body:', body);
    }
});

  1. 导入 request 库:request 是一个用于发起 HTTP 请求的库。

  2. 定义代理信息:proxyHost 和 proxyPort 是服务器的主机名和端口号。

  3. 定义要爬取的网站的 URL:url 是要爬取的网站的 URL。

  4. 使用 request 库发起请求:request 方法接受三个参数:url,method 和 proxy。url 是要请求的 URL,method 是请求的方法(如 GET,POST 等),proxy 是服务器的配置。