1. 程式人生 > >Python爬蟲——代理伺服器進行資訊的爬取

Python爬蟲——代理伺服器進行資訊的爬取

利用代理伺服器進行資訊的爬取以防止自己的IP的被伺服器封
def function1(url,IP):#url  IP
    """代理伺服器進行資訊的爬取以防止自己的IP的被伺服器封"""
    proxy=urllib.request.ProxyHandler({"http":IP})#http:IP:埠
    opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
    urllib.request.install_opener(opener)#新增全域性
    data=urllib.request.urlopen(url).read().decode("utf-8","ignore")
    print(len(data))
    print("爬蟲代理伺服器結束")
    return

if __name__ == '__main__':
    url="http://www.baidu.com"
    IP="110.179.64.194:8123"#這是代理伺服器的問題需要不斷的進行調整免費的IP地址
    function1(url,IP)

免費的代理的IP地址我已經附上鍊接自己去改動就好;http://www.xicidaili.com/