#coding=utf-8
'''接口请求时做IP代理'''
import requests
#请求地址
targetUrl = "https://www.cnblogs.com/"
#代理服务器
proxyHost = "xxx.xxx.xxx.xx"
proxyPort = "xxxx"
proxyMeta = "http://%(host)s:%(port)s" % {
"host" : proxyHost,
"port" : proxyPort,
}
#pip install -U requests[socks] socks5
# proxyMeta = "socks5://%(host)s:%(port)s" % {
# "host" : proxyHost,
# "port" : proxyPort,
# }
proxies = {
"http" : proxyMeta,
"https" : proxyMeta
}
resp = requests.get(targetUrl, proxies=proxies)
print(resp.status_code)
print(resp.text)
'''selenium 使用IP代理'''
from selenium import webdriver
#代理服务器
proxyHost = "xxx.xxx.xxx.xx"
proxyPort = "xxxx"
chome_options = webdriver.ChromeOptions()
chome_options.add_argument(('--proxy-server=http://' + proxyHost+':'+proxyPort))
profile_directory = r'D:\chromedriver_win32\chromedriver.exe'
driver=webdriver.Chrome(profile_directory,chrome_options=chome_options)
driver.get(url='https://ip138.com/')
driver.get(url='https://www.zhihu.com/signin?next=%2F') #打开知乎