【问题标题】:Webscraper being blocked--how to make a Puppeteer IP address rotation?Webscraper 被阻止-如何进行 Puppeteer IP 地址轮换?
【发布时间】:2021-05-17 12:14:33
【问题描述】:

所以在我的网络爬虫功能中,我有以下代码行:

let portList = [9050, 9052, 9053, 9054, 9055, 9056, 9057, 9058, 9059, 9060];
let spoofPort = portList[Math.floor(Math.random()*portList.length)];
console.log("The chosen port was " + spoofPort);

const browser = await puppeteerExtra.launch({ headless: true, args: [                
'--no-sandbox', '--disable-setuid-sandbox', '--proxy-server=socks5://127.0.0.1:' + spoofPort                                               
]});

const page = await browser.newPage();

const userAgent = 'Mozilla/5.0 (X11; Linux x86_64)' +           
      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36';

await page.setUserAgent(userAgent);

我正在尝试轮换每个请求的 IP 地址(包含此代码的函数本质上是在来自客户端的每个请求上调用),这样我就不会被抓取的网站这么快阻止。我收到以下错误:

2021-05-17T12:08:19.625349+00:00 app[web.1]: The chosen port was 9050
2021-05-17T12:08:20.042016+00:00 app[web.1]: Error: net::ERR_PROXY_CONNECTION_FAILED at https://expampleDomanPlaceholder.com
2021-05-17T12:08:20.042018+00:00 app[web.1]: at navigate (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:115:23)
2021-05-17T12:08:20.042018+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:93:5)
2021-05-17T12:08:20.042019+00:00 app[web.1]: at async FrameManager.navigateFrame (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:90:21)
2021-05-17T12:08:20.042020+00:00 app[web.1]: at async Frame.goto (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:416:16)
2021-05-17T12:08:20.042021+00:00 app[web.1]: at async Page.goto (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:819:16)
2021-05-17T12:08:20.042021+00:00 app[web.1]: at async /app/app.js:174:9

我已经尝试了这些帖子中详述的解决方案,但问题可能出在我的 userAgent 上?:

Getting error when attempting to use proxy server in Node.js / Puppeteer

https://github.com/puppeteer/puppeteer/issues/2472

更新:我尝试使用此 buildpack (https://github.com/iamashks/heroku-buildpack-tor-proxy.git),但它一直导致我的 web dyno 中断(返回“H14”错误,这意味着您必须清除构建包并重新添加它们)。不知道如何从这里开始,因为这似乎是我能遇到的唯一解决方案。

【问题讨论】:

  • 日志中的错误是正确的:net::ERR_PROXY_CONNECTION_FAILED看来是Tor没有配置on not working。
  • @Vaviloff 在某些情况下,我正在部署到 Heroku 并在 Mac 上的 Node.js 环境中工作。查看此链接 (medium.com/@jsilvax/running-puppeteer-with-tor-45cc449e5672),您似乎对我没有下载 Tor 是正确的。但是如果我要部署到 Heroku,我如何确保 tor 工作?我要安装这个包还是什么:npmjs.com/package/tor-request
  • @Vaviloff 你有什么建议吗?
  • 我建议您搜索 using tor on heroku 之类的内容,然后相应地调整您的应用
  • @Vaviloff 所以我尝试将 Tor 构建包从您的链接添加到我的 Heroku 应用程序,但仍然无法让我的代码正常工作。我还尝试了一堆其他“免费代理”掩码,但它们都不起作用(包括 puppeteer-page-proxy 和 get-free-https-proxy)。你知道有谁在你可以联系我之前将 Tor 部署到 Heroku 吗?

标签: javascript node.js web-scraping proxy puppeteer


【解决方案1】:

所以有几个问题。

  1. 发布的错误消息缺少占位符
  2. 该请求因拼写错误而失败。
  3. 您必须将代理服务器实际提供给浏览器对象。它必须被初始化。
Error: net::ERR_PROXY_CONNECTION_FAILED at https://expampleDomanPlaceholder.com

这里是柬埔寨的代理服务器示例

We will use SOCKS4 proxy and IP location of this proxy at Cambodia.
Proxy IP address 96.9.77.192 and port 55796 (not sure if it still works) 


const puppeteer = require('puppeteer');

(async () => {
    let launchOptions = { headless: false, 
                          args: ['--start-maximized',
                                 '--proxy-server=socks4://96.9.77.192:55796'] // this is where we set the proxy
                        };

    const browser = await puppeteer.launch(launchOptions);
    const page = await browser.newPage();

    // set viewport and user agent (just in case for nice viewing)
    await page.setViewport({width: 1366, height: 768});
    await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36');

    // go to whatismycountry.com to see if proxy works (based on geography location)
    await page.goto('https://whatismycountry.com');

    // close the browser
    await browser.close();
})();

#Proxy Issue
If the proxy host requires AUTH then the example below would be more fitting. 


'use strict';

const puppeteer = require('puppeteer');

(async () => {
  const username = process.env.USER
  const password = process.env.PASS
  const url = 'https://www.google.com'

  const browser = await puppeteer.launch({
    # proxy host must be correct.
    args: [
      '--proxy-server=socks5://proxyhost:8000',
    ],
  });

  const page = await browser.newPage();

  await page.authenticate({
    username,
    password,
  });

  await page.goto(url);

  await browser.close();
})();

this worked with tor. 
 Tor ('--proxy-server=socks5://localhost:9050')

参考资料: 感谢@Grant Miller 的 TOR 测试。

https://dev.to/sonyarianto/practical-puppeteer-using-proxy-to-browse-a-page-1m82

How to make puppeteer work through socks5 proxy?

【讨论】:

  • 占位符是我不想放入堆栈的谷歌搜索结果。我认为您提供的 IP/端口组合不再启动并运行,所以我只是查找了一个基于美国的免费 IP/端口组合并找到 (socks4://98.162.25.23:4145)。我尝试将其放入 --proxy -server 标记中,但现在在 .goto() 行出现“错误:net::ERR_SSL_PROTOCOL_ERROR”。我尝试了多个代理并尝试看到 ignoreHTTPSErrors: true。我想知道,谷歌会阻止 socks5 传输协议吗?你有什么建议吗?
  • 另外,我实际上如何 npm install Tor(或我必须做的任何事情)?为 heroku/Node.js 下载 Tor 的文档似乎非常有限......
猜你喜欢
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
相关资源
最近更新 更多