0

Splash 浏览器不会通过 http 代理发送任何内容。即使代理未运行,也会获取页面。

在对 Angular.js 网站进行身份验证后,我在 python 3 中使用带有 splash 的 scrapy 来获取页面。该脚本能够获取页面、验证和验证后获取页面。但是,它不使用 localhost:8090 的代理设置,wireshark 确认来自端口 8050 的流量会流向 50k 范围内的某个端口。

设置是 - 在端口 8050 上的 docker 映像(最新)上本地运行的飞溅 - 在 mac 上本地运行的 python 3 - 在端口 8090 的 mac 上本地运行的 Zap 代理 - 通过 VPN 访问的网页

我尝试使用带有 LUA 脚本的 Chrome 通过服务器指定代理主机:端口。页面是在没有代理的情况下获取的。

我试图在 python 脚本中使用 Lua 和 api (args={'proxy':'host:port'} 指定代理,并且在不使用代理的情况下获取页面。

我尝试使用代理主机文件,我得到状态 502。

  1. 在 Chrome 上通过 Lua 设置代理(无错误,未代理):
function main(splash, args)
  splash:on_request(function(request)
    request:set_proxy{
      host = "127.0.0.1",
      port = 8090,
      username = "",
      password = "",
      type = "HTTP"
    }
  end
  )
  assert(splash:go(args.url))
  assert(splash:wait(0.5))

  return {
    html = splash:html(),
    png = splash:png(),
    har = splash:har(),
  }
end

req = SplashRequest("http://mysite/home", self.log_in,
                     endpoint='execute', args={'lua_source': script})
  1. 通过 api 设置的代理(状态 502):
req = SplashRequest("http://mysite/home",
                            self.log_in, args={'proxy': 'http://127.0.0.1:8090'})
  1. 在 Python 中通过 Lua 设置代理(无错误,未代理):
def start_requests(self):
        script = """
            function main(splash, args)

                assert(splash:go(args.url))
                assert(splash:wait(0.5))
                splash:on_request(function(request)
                    request:set_proxy{
                        host = "127.0.0.1",
                        port = 8090,
                        username = "",
                        password = "",
                        type = "HTTP"
                    }
                end
                )

                return {
                    html = splash:html(),
                    png = splash:png(),
                    har = splash:har(),
             }
            end
            """
        req = SplashRequest("http://mysite/home", self.log_in,
                            endpoint='execute', args={'lua_source': script})
        # req.meta['proxy'] = 'http://127.0.0.1:8090'
        yield req
  1. 通过 docker 镜像中的代理文件设置代理(状态 502):代理文件:
[proxy]

; required
host=127.0.0.1
port=8090

外壳命令:

docker run -it -p 8050:8050 -v ~/Documents/proxy-profile:/etc/splash/proxy-profiles scrapinghub/splash --proxy-profiles-path=/etc/splash/proxy-profiles

以上所有内容都应在端口 8090 的 zap 代理中显示页面。

上面的一些似乎设置了代理,但是代理无法到达localhost:8090(状态502)。有些根本不起作用(没有错误,没有代理)。我认为这可能与正在使用 docker 映像有关。

我不打算使用 Selenium,因为这就是它的替代品。

4

2 回答 2

2

返回状态 502 的所有方法都正常工作。出现此问题的原因是 docker 镜像无法访问主机上的 localhost。要解决此问题,http://docker.for.mac.localhost:8090请在 mac 主机上用作代理 host:port,并docker run -it --network host scrapinghub/splash在 linux 上使用 localhost:port。对于 linux,-p 无效,因为容器上的所有服务都将在 localhost 上。

方法 2 最适合没有规则的单个代理。方法 4 最适合具有规则的多个代理。

我没有尝试其他方法来查看这些更改会返回什么以及为什么。

于 2019-08-01T17:23:49.343 回答
0

好吧,我已经为同样的问题苦苦挣扎了一段时间,但我在 GitHub 上找到了您的第一种方法的解决方案,该方法基于Docker 文档状态

主机有一个不断变化的 IP 地址(如果您没有网络访问权限,则没有)。从 18.03 开始​​,我们的建议是连接到特殊的 DNS 名称 host.docker.internal,它解析为主机使用的内部 IP 地址。网关也可以通过 gateway.docker.internal 访问。

这意味着您应该/可以使用“host.docker.internal”作为您的代理的主机,例如

splash:on_request(function (request)
     request:set_proxy{
         host = "host.docker.internal",
         port = 8090
     }
end)

这是解释的链接:https ://github.com/scrapy-plugins/scrapy-splash/issues/99#issuecomment-386158523

于 2020-05-30T16:10:22.517 回答