如何为 Crawlee 设置代理
Proxy Port SDK 包含 Crawlee 的代理轮换包。 使用这个包你不需要担心代理轮换,一切都会自己完成。 如果您想手动设置代理,请继续阅读。
您所有的代理需求都由
您需要实例化
ProxyConfiguration
类管理。您需要实例化
ProxyConfiguration
类并将其传递给 Crawler
构造函数。您可以从预定义列表中设置代理:
import { CheerioCrawler, ProxyConfiguration } from 'crawlee'; const proxyConfiguration = new ProxyConfiguration({ proxyUrls: [ // 将其替换为您的代理服务器的 URL 'http://yourproxyserver-1.com', 'http://yourproxyserver-2.com', ], }); const crawler = new CheerioCrawler({ proxyConfiguration, // ... });
或者使用动态提供代理的函数:
import { CheerioCrawler, ProxyConfiguration } from 'crawlee'; async function newUrlFunction(sessionId: string | number): Promise<string> { // 必须由您定义 return getProxyFunction(); } const proxyConfiguration = new ProxyConfiguration({ newUrlFunction: newUrlFunction, }); const crawler = new CheerioCrawler({ proxyConfiguration, // ... });
也可以看看o:
crawlee-proxyport
- Crawlee 的代理提供商