爬虫之添加代理(五)

验证代理是否可用

    try:
        telnetlib.Telnet('221.15.192.96', port='1080', timeout=3)
    except:
        print('ip无效!')
    else:
        print('ip有效!')

添加代理

添加proxy属性

import requests
from random import choice
proxies= [
    'http://196.52.58.239:80',
    'http://165.225.112.77:10605',
    'http://165.225.76.70:10605',
    'http://122.226.57.70:8888',
    'http://123.57.84.116:8118',
    'http://202.109.157.64:9000',
    'http://139.155.41.15:8118',
    'http://39.106.223.134:80',
    'http://47.115.63.52:8888'
]
proxy = choice(proxies)
session = requests.Session()
page_text = session.get(url=url, proxy=proxy, headers=headers)

你可能感兴趣的:(proxy,python,url)