一、什么是HTTP代理。
HTTP代理就是介于瀏覽器和web服務(wù)器之間的一臺服務(wù)器,連接代理后,瀏覽器不再直接向web服務(wù)器取回網(wǎng)頁,而是向代理服務(wù)器發(fā)出request信號,代理服務(wù)器再想web服務(wù)器發(fā)出請求,收到web服務(wù)器返回的數(shù)據(jù)后再反饋給瀏覽器。
二、華益云HTTP代理IP基本參數(shù)介紹
1:基本參數(shù)介紹:支持http,https,socks5等多種協(xié)議類型。100%高匿IP,支持API提取,API最快提取間隔1秒,一次最多可以提取200個。
2:支持的語言:python,java,c,c#,go,php,易語言等多種編程語言。
3:使用范圍:軟件程序API調(diào)用,爬蟲,網(wǎng)頁訪問,瀏覽等支持HTTP協(xié)議批量調(diào)用的應(yīng)用。
python代碼中如何使用HTTP代理。
# 此版本無需安裝依賴
import urllib
import urllib.request
import urllib
def main():
# 發(fā)送給服務(wù)器的標識
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/532.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
# 代理api(這里我推薦使用www.9vps.com華益云的HTTP代理API,注冊就白嫖1萬IP)
proxyUrl = "http://http.9vps.com/getip.asp?username=166xxxx6597&pwd=xxxxbaa59ce237dff65134984b9cxxxx&geshi=1&fenge=1&fengefu=&Contenttype=1&getnum=20&setcity=&operate=all&";
# 請求代理url,獲取代理ip
outPutProxy = getProxy(proxyUrl, userAgent)
if len(outPutProxy)==0:
# 沒有獲取到代理
return
# 目標請求網(wǎng)站
# https://httpbin.org/get
url = "https://www.qq.com/"
content = None
for _ in range(0, 3):
# 最多嘗試三次
try:
# 從列表中取出一個代理出來
proxy = outPutProxy.pop(0)
px = {
"http": proxy,
"https": proxy
}
content = requestGet(url, userAgent, px)
break
except Exception as e:
print(e)
if (len(outPutProxy) == 0):
# 如果發(fā)現(xiàn)沒有代理了,就去獲取下。
outPutProxy = getProxy(proxyUrl, userAgent)
print(content)
def getProxy(proxyUrl, userAgent):
proxyIps=""
outPutProxy = []
try:
proxyIps = requestGet(proxyUrl, userAgent, None)
print("(proxyIps)", proxyIps)
# {"code":3002,"data":[],"msg":"error!用戶名或密碼錯誤","success":false}
if "{" in proxyIps:
raise Exception("[錯誤]"+proxyIps)
outPutProxy = proxyIps.splitlines()
except Exception as e:
print(e)
print("總共獲取了"+str(len(outPutProxy))+"個代理")
return outPutProxy
def requestGet(url, userAgent, proxy):
headers = {
"User-Agent": userAgent
}
# httpproxy_handler = urllib.ProxyHandler({"http" : " 180.104.192.217:22036"})
response = None
if (proxy):
proxyHandler = urllib.request.ProxyHandler(proxy)
opener = urllib.request.build_opener(proxyHandler, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request, timeout=5)
else:
# 沒有代理走這個
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request, timeout=5)
#response = opener.open(request)
html = response.read()
# # 設(shè)置編碼,防止亂碼
# 手動設(shè)置網(wǎng)頁字符編碼方式
return html.decode("utf-8", "ignore")
main()
-
HTTP
+關(guān)注
關(guān)注
0文章
530瀏覽量
34439 -
瀏覽器
+關(guān)注
關(guān)注
1文章
1042瀏覽量
36791 -
python
+關(guān)注
關(guān)注
56文章
4848瀏覽量
88926
發(fā)布評論請先 登錄
基礎(chǔ)篇3:掌握Python中的條件語句與循環(huán)
零基礎(chǔ)入門:如何在樹莓派上編寫和運行Python程序?

評論