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

鍍金池/ 問答/Python/ 關(guān)于爬蟲代理IP的問題

關(guān)于爬蟲代理IP的問題

# -*- coding:utf-8 -*-
import urllib.request
import re
url = "http://ip.chinaz.com/getip.aspx"  #打算抓取內(nèi)容的網(wǎng)頁
proxy_ip={'HTTP':'49.85.13.8:35909'}  #想驗(yàn)證的代理IP
proxy_support = urllib.request.ProxyHandler(proxy_ip)
opener = urllib.request.build_opener(proxy_support)
opener.addheaders=[("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64)")]
urllib.request.install_opener(opener)
#shuchu = re.findall('"well"><p>(.*?)GeoIP', urllib.request.urlopen(url).read().decode("utf-8"), re.S)
#print(shuchu)
print(urllib.request.urlopen(url).read().decode("utf-8"))

我用的是3.6的版本。代碼也是網(wǎng)上找的模版,按道理來說應(yīng)該是沒有什么問題的把,我也試過很多遍網(wǎng)上的免費(fèi)IP,結(jié)果還是顯示自己的IP。

回答
編輯回答
膽怯

代理ip的格式不對,{'http': 'http://proxy-ip:port'}

2018年1月27日 17:38
編輯回答
笨笨噠

這個代理IP數(shù)據(jù)類型為字典,如果是http協(xié)議,key值就為"http",value值應(yīng)為"代理IP:端口號"的格式,歡迎參考Python爬蟲學(xué)習(xí)之(二)| urllib進(jìn)階篇

proxy = {'http': '115.193.101.21:61234'}

# 使用ProxyHandler方法創(chuàng)建proxy處理器對象
proxy_handler = urllib.request.ProxyHandler(proxy)

# 創(chuàng)建代理IP的opener實(shí)例,參數(shù)為proxy處理器對象
opener = urllib.request.build_opener(proxy_handler)

# 用代理IP的opener打開指定狀態(tài)的URL信息
html = opener.open(response)

2017年3月23日 09:29