思路:
先在从网上爬取一个代理列表,做代理池,并保存在本地http_list.txt和https_list.txt中,然后从中随机池取出一个ip做代理。
本代码是从用webdriver模拟chrome浏览器
http://www.xicidaili.com/上爬取的代理的(因为这个网站有发爬虫,就直接用webdriver快速搞定,这么一个小网站,就懒得去研究它的反爬了)。
爬取成功会返回响应代码200。
import random,requests,os
from selenium import webdriver
from lxml import etree
def req(url):
# 设置多个 user-agent
User_Agent = random.choice([
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
"Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/5.0 (Windows; U; MSIE 9.0; Wind