从代理网上获取代理IP之后,还得查看代理是否可用.有两种方法进行测试

  1. 利用requests库挂上代理访问网站,监测是否访问成功
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import requests

    proxies={
    "http":"http://185.44.15.228:3130",
    "https":"http://185.44.15.228:3130",
    }

    try:
    requests.get('http://www.baidu.com', proxies=proxies)
    except:
    print('failed')
    else:
    print('success')
  2. 利用telnetlib库对代理ip进行测试,监测是否成功
    1
    2
    3
    4
    5
    6
    7
    import telnetlib
    try:
    tel=telnetlib.Telnet('121.199.25.64',port='80',timeout=10)
    except:
    print('failed')
    else:
    print('success')