美文网首页
爬取西刺代理的免费IP并测验是否可用

爬取西刺代理的免费IP并测验是否可用

作者: 裴general | 来源:发表于2018-07-07 13:54 被阅读0次

    经过本人的测试, 知名的网站基本上都会封禁西刺代理上的ip,当然也不排除有漏网之鱼, 如果大家要大量爬取知名网站的数据, 建议使用其他有效的代理IP.

    import re
    import requests
    from bs4 import BeautifulSoup
    import time
    import random
    
    
    # 测试是否可用
    # ip为传入的ip :  0.0.0.0:8000
    # test_url 为测试的目标网站, 可以看作你将要爬取得网站
    # tiem_out 请求的延迟
    def test_ip(ip, test_url='https://movie.douban.com/', time_out=0.3):
        proxies = {'https': ip} # 代理
        global all # 使用全局变量
        j = 0
        while j < 3: # 一共测试3次
            try:
                # 请求目标网址
                r = requests.get(test_url, proxies=proxies, timeout=time_out)
                # 如果返回的状态码为200, 则表示成功
                if r.status_code == 200:
                    print('***************测试通过%s**************' % ip)
                    all.append(ip) # 将通过的ip加入到列表中
                    break
                else:
                    print('请求失败%s' % ip)
            except:
                print('请求过程错误%s' % ip)
            j += 1
            print('-----------------这是第  %d 次测试----------------' % j)
    
    url = 'http://www.xicidaili.com/nn/'
    # 请求头池, 也是防止反爬手段的一种
    user_list = ["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; Windows NT 9.0; en-US)",
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
        "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
        "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
        "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3"]
    # 设置全局变量, 用来存取测试通过的IP
    all = []
    # 一个装爬下来IP的容器
    ip_list = []
    # 爬取20页的数据
    for page in range(20):
        url += str(page)
        headers = {
            # 每一次爬取选择随机的请求头
            'User-Agent': random.choice(user_list)
        }
        #每一次爬取随机暂停秒数, 采用是随机浮点数
        time.sleep(random.uniform(0, 4))
      # 解析西刺代理的页面结构
        res = requests.get(url, headers=headers).text
        soup = BeautifulSoup(res, 'lxml')
        ips = soup.find_all('', {'class': 'odd'})
        for ip_ in ips:
            ip = re.findall(r'<td>(.*)</td>', str(ip_))
            ip_list.append(ip[0] + ':' + ip[1])
            print(ip[0] + ':' + ip[1])
    
    # 测试爬取得每个ip
    for ip in ip_list:
        test_ip(ip)
    # 打印出测试通过的ip, 当然也可以持久化到数据库中或者存放到本地中
    print(all)
    

    相关文章

      网友评论

          本文标题:爬取西刺代理的免费IP并测验是否可用

          本文链接:https://www.haomeiwen.com/subject/ugswuftx.html