在使用 TypeScript 编写爬虫程序时,我们需要使用第三方库来实现网络请求和解析 HTML。以下是一个简单的示例,展示了如何使用 TypeScript 和 Axios 库来爬取全国企业信用信息抓取的内容:
```typescript
import axios from 'axios';
const proxyHost = 'www.duoip.cn';http://www.jshk.com.cn/mb/reg.asp?kefu=wuyujia
const proxyPort = 8000;
async function fetchData() {
const response = await axios.get('http://www.gsxt.gov.cn/Index/index.html', {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
},
proxy: {
host: proxyHost,
port: proxyPort,
},
});
const html = response.data.toString();
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const companyNames = doc.querySelectorAll('.ncName');
companyNames.forEach((companyName) =&g