代理服务器的使用 之 urllib

# -*- coding: utf-8 -*-
"""
Created on Mon May 28 15:04:05 2018

@author: Administrator
"""

import urllib
import re



def chechProxy(proxy_addr):
    data = ""
    url = "http://www.baidu.com"
    proxy = urllib.request.ProxyHandler({"http":proxy_addr})
    # 替换handler,以实现可以处理Proxy
    opener = urllib.request.build_opener(proxy)
    # 把opener装载进urllib库中,准备使用
    urllib.request.install_opener(opener)
    try:
        response = urllib.request.urlopen(url, timeout=10)   
        data = response.read().decode('utf-8')
    except:
        pass
    
    pattern = re.compile("<title>百度一下,你就知道</title>")
    title = re.findall(pattern, data)
    print(title)
    if len(title) == 0:#list的长度为0,说明没有获取到信息
        return False
    else:
        return True
    
print(chechProxy("185.106.121.98:1080"))
                 #"name:password@ip:port"    

猜你喜欢

转载自blog.csdn.net/qq_35810838/article/details/84674082