在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ Python 爬蟲 設(shè)置代理 IP地址未改變

Python 爬蟲 設(shè)置代理 IP地址未改變

python版本:3.6.5
使用網(wǎng)上買的代理,通過shadowsocks來連接的代理如下:
clipboard.png

最后得到的ip為本地ip,在網(wǎng)上百度了很多也不知道為啥

代碼如下:

import urllib.request
from urllib.request import ProxyHandler,build_opener
p=urllib.request.ProxyHandler({'https':'https://ru.loli.lc:16103'})
o=urllib.request.build_opener(p)
req=urllib.request.Request('http://httpbin.org/get')
r=o.open(req)
print (r.read())

輸出結(jié)果:
b'{"args":{},"headers":{"Accept-Encoding":"identity","Connection":"close","Host":"httpbin.org","User-Agent":"Python-urllib/3.6"},"origin":"180.162.61.251","url":"http://httpbin.org/get"}n'
https://ru.loli.lc:16103 改成https://ru.loli.lc:1080 還是不行

回答
編輯回答
青瓷

shadowsocks 客戶端會在本地 1080 端口監(jiān)聽 socks5 協(xié)議的流量。

所以正確的代理配置是:

proxies = {
    'http': 'socks5://127.0.0.1:1080',
    'https': 'socks5://127.0.0.1:1080'
}

另外,requests 比 urllib 更優(yōu)雅

pip install requests[socks]
import requests


proxies = {
    'http': 'socks5://127.0.0.1:1080',
    'https': 'socks5://127.0.0.1:1080'
}
requests.get('http://httpbin.org/get', proxies=proxies).content
2018年3月17日 05:09