python随机切换代理请求数据

使用免费代理ip请求数据,代理ip的获取,详见 “爬取站大爷的免费ip代理” 篇

# coding = utf-8
import random
from time import sleep, ctime
import time
import requests

url = 'http://linuxdba.ltd'
proxies_list = [{'http': 'http://59.55.161.88:3256', 'https': 'https://59.55.161.88:3256'},
                {'http': 'http://103.37.141.69:80', 'https': 'https://103.37.141.69:80'},
                {'http': 'http://27.191.60.168:3256', 'https': 'https://27.191.60.168:3256'},
                {'http': 'http://124.205.153.36:80', 'https': 'https://124.205.153.36:80'},
                {'http': 'http://139.224.18.116:80', 'https': 'https://139.224.18.116:80'},
                {'http': 'http://60.191.11.241:3128', 'https': 'https://60.191.11.241:3128'},
                {'http': 'http://120.194.55.139:6969', 'https': 'https://120.194.55.139:6969'},
                {'http': 'http://175.7.199.222:3256', 'https': 'https://175.7.199.222:3256'},
                {'http': 'http://27.191.60.5:3256', 'https': 'https://27.191.60.5:3256'},
                {'http': 'http://121.4.36.93:8888', 'https': 'https://121.4.36.93:8888'}]

header = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Mobile Safari/537.36'

for i in range(50):  # 请求50次,随机挑选代码,也可轮询检测代理有效性,将无效的剔除后再使用
    try:
        proxies = random.choice(proxies_list)
        print(proxies)
        res = requests.get(url, headers={"User-Agent": header}, proxies=proxies, timeout=2)
        print(res.status_code)
        print(res.request.headers)
    except Exception as e:
        print(e)

结果域名tomcat日志:

python随机切换代理请求数据_第1张图片

 

你可能感兴趣的:(python,python,爬虫)