如何使用 Socks5 代理抓取请求?



>问题:

代理如何使用socks5scrapy请求?

我知道我可以使用polipoSocks代理转换为Http代理

但:

我想设置中间件或对scrapy.Request进行一些更改

import scrapy
class BaseSpider(scrapy.Spider):
"""a base class that implements major functionality for crawling application"""
start_urls = ('https://google.com')
def start_requests(self):
proxies = {
'http': 'socks5://127.0.0.1:1080',
'https': 'socks5://127.0.0.1:1080'
}
for url in self.start_urls:
yield scrapy.Request(
url=url,
callback=self.parse,
meta={'proxy': proxies} # proxy should be string not dict
)
def parse(self, response):
# do ...
pass

我应该为变量分配什么proxies

这是可能的。

HTTP 代理到 Socks5

安装蟒蛇代理

$ pip3 install pproxy

$ pproxy -l http://:8181 -r socks5://127.0.0.1:9150 -vv

使用HTTP代理刮擦

创建中间件 (middlewares.py(

class ProxyMiddleware(object):
def process_request(self, request, spider):
request.meta['proxy'] = "http://127.0.0.1:8181"

将其分配给DOWNLOADER_MIDDLEWARES(settings.py(

DOWNLOADER_MIDDLEWARES = {
'PROJECT_NAME_HERE.middlewares.ProxyMiddleware': 350
}

目前不可能。有一个功能请求。

中间件可以是:

class ProxyMiddleware(object):
def process_request(self, request):
request.meta['proxy'] = "socks5://127.0.0.1:1080"

使其在您的 settings.py 文件中可用,看看它是否有效。

看看它,如果它对 https://github.com/gregoriomomm/docker-multsocks 有帮助......

它提供了一种连接到HTTP PROXY标准协议的方法(就像Docker一样多平台(,以访问具有高级路由配置的SOCKS5服务器,并非所有软件都免费提供这种配置,例如在Windows中,您可以在本地配置简单的HTTP代理(请参阅底部的配置(。

在这种情况下,它也可以用于许多应用程序,例如一些旧的 java 实现,它们可以连接到 SOCKS,但未启用以正确传递用户和密码以验证 SOCKS 连接,因此它可以充当 SOCKS,而无需身份验证链接到经过身份验证的。

它基于常见的 linux 命令,也可以在 Windows 10 中重现,方法是在 Windows 子系统 Linux (WSL( 的 shell 上使用相同的命令。

在 Ubuntu 中,您只需安装它即可

sudo apt install tsocks nmap
# Once you have a tsocks installed and configured 
echo "Starting http proxy!!!"
tsocks ncat -l --proxy-type http localhost 3128 & 

/etc/tsocks.conf 文件的示例(将变量替换为"v"(:

local = 9.0.0.0/255.0.0.0
local = 129.39.186.192/255.255.255.192
path {
reaches = 10.0.0.0/255.0.0.0
reaches = 158.98.181.232/255.255.255.248
reaches = 192.168.0.0/255.255.0.0
server = vSOCKS_HOST
server_port = vSOCKS_PORT
server_type = 5
default_user = vSOCKS_USERNAME
default_pass = vSOCKS_PASSWORD
fallback = yes
}

如果你想尝试docker版本,只需更改path/tsocks.conf 到您的版本,它将加载HTTP SOCKS和SOCKS5未经身份验证的路由到您的SOCKS5最终目标服务器(还有其他选项(

docker run -v path/tsocks.conf:/etc/tsocks.conf -p 3128:3128 -p 1080:1080  gregoriomomm/multsocks:latest 

在 https://github.com/gregoriomomm/tsocks 有一个版本的tsocks(http://tsocks.sourceforge.net/(来启用tsocks(透明SOCKS5代理库(,对使用Alpine:3.11工作和编译进行了细微调整,并包括与Ubuntu相同的回退选项。

最新更新