就是這樣的:
crawl_config = {
? ? ? ? "proxy":"用戶名:密碼@你的代理池ip:6666"
}
首先要安裝squid(centos下)
yum install -y squid
作者都推薦squid,那么就用吧~
添加squid的用戶驗(yàn)證
htpasswd -c /etc/squid/passwd 用戶名
根據(jù)提示輸入兩次密碼
如果找不到htpasswd, 執(zhí)行
yum install httpd
然后繼續(xù)上一步。
然后根據(jù)這個(gè)配置文件覆蓋/etc/squid/squid.conf文件
http_port 6666 #對外公布的端口
#緩存大小
cache_mem 128 MB
maximum_object_size 16 MB
cache_dir ufs /var/spool/squid 100 16 256
access_log /var/log/squid/access.log
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd #指定認(rèn)證程序以及賬戶文件
#auth_param basic children 50 #同時(shí)連接的客戶端數(shù)量
# 定義端口
acl Safe_ports port 80? ? ? # http
acl SSL_ports port 443
acl CONNECT method CONNECT
acl auth_user proxy_auth REQUIRED
# 拒絕所有非定義的端口?
http_access deny !Safe_ports
http_access allow auth_user
# 拒絕所有非定義的端口
http_access deny CONNECT !SSL_ports
http_access deny all
via off
forwarded_for delete
#forwarded_for off
#文件最后加上? 高匿配置?
request_header_access X-Forwarded-For deny all?
request_header_access From deny all?
request_header_access Via deny all
follow_x_forwarded_for deny all
request_header_access Referer deny all
request_header_access User-Agent deny all
cache_peer 115.213.238.102 parent 35759 0 no-query weighted-round-robin weight=1 connect-fail-limit=2 allow-miss max-conn=5 name=115.213.238.10220
never_direct allow all
其中這里就是你可用的http 代理ip列表
cache_peer 115.213.238.102 parent 35759 0 no-query weighted-round-robin weight=1 connect-fail-limit=2 allow-miss max-conn=5 name=115.213.238.10220
網(wǎng)上免費(fèi)的質(zhì)量都不高,有銀子的建議買收費(fèi)的~
如果你使用的是pyspider的話吝秕,還可以直接建立一個(gè)任務(wù),定時(shí)爬取某個(gè)免費(fèi)的代理列表惠豺。然后通過任務(wù)定時(shí)生成squid.conf文件绊袋。
以下是代碼片段蟋恬,大家參考一下
先復(fù)制一份squid.conf.example當(dāng)做模板涤久∥谐荆可以參考如下代碼:
cd /etc/squid
mv squid.conf.default squid.conf.example
以下是更新conf的代碼:
????????proxy_list = response.json['msg']#獲取到的ip列表
????????default_conf = open('/etc/squid/squid.conf.example' , 'r').read()#讀取模板文件
? ? ? ? default_conf += '\n'
? ? ? ? for index? in range(len(proxy_list)):#把ip定制成指定格式
? ? ? ? ? ? ip, port? = proxy_list[index]['ip'], ?proxy_list[index]['port']
? ? ? ? ? ? proxy_conf = "cache_peer " + ip + " parent " + str(port) + " 0 no-query weighted-round-robin weight=2 connect-fail-limit=2 allow-miss max-conn=5 name=proxy-" + str(index) + "\n"
? ? ? ? ? ? default_conf += proxy_conf
? ? ? ? default_conf += '\n never_direct allow all'
? ? ? ? conf = open('/etc/squid/squid.conf' , 'w')#存儲(chǔ)文件
? ? ? ? conf.write(default_conf)
? ? ? ? conf.close()
? ? ? ? message = os.system('systemctl restart squid')
--------------