美文网首页
python3 使用免费代理 IP 刷网站的访问量

python3 使用免费代理 IP 刷网站的访问量

作者: lightandall | 来源:发表于2019-11-05 09:50 被阅读0次

使用网络上免费的代理ip,让你的python浏览其它网站的时候不显示真实的ip,尤其是爬虫的时候,可以定时换ip。非常方便。
使用python模拟真实的浏览器去定时访问要刷量的网站,加上代理IP。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#-*- maxc.cc - 2019-09-18  -*-

import requests
from bs4 import BeautifulSoup
import random
import time

proxiesurl = 'http://www.xicidaili.com/nt/'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}   

#获取代理ip的list,用于随机选一个
def get_ip_list(url, headers):
    web_data = requests.get(url, headers=headers)
    soup = BeautifulSoup(web_data.text, 'lxml')
    ips = soup.find_all('tr')
    ip_list = []
    for i in range(1, len(ips)):
        ip_info = ips[i]
        tds = ip_info.find_all('td')
        ip_list.append(tds[1].text + ':' + tds[2].text)
    print('get ip list success')
    return ip_list

#随机从获取到的IP中选一个生成代理IP
def get_random_ip(ip_list):
    proxy_list = []
    for ip in ip_list:
        proxy_list.append('http://' + ip)
    proxy_ip = random.choice(proxy_list)
    prox = {'http': proxy_ip}
    print('get random ip - success')
    return prox

#刷PV的主程序
def mainpv():
    pvurl = ['https://mikeyoung.zcool.com.cn/',
       'https://www.zcool.com.cn/work/ZMTM0Mzk2MjA=.html']
    count = 0
    countUrl = len(pvurl) 
    print(proxies)

    #设置一个随机执行的时间
    randomtime = random.randint(1, 100)
    print(randomtime)

    # 访问次数设置限制
    while count < 11:
        try:  # 正常运行
            for i in range(countUrl):
                response = requests.get(pvurl[i], headers=headers,proxies=proxies)
                if response.status_code == 200:
                    count = count + 1
                    print('Success ' + str(count), 'times')
            time.sleep(randomtime)
        except Exception:  # 异常
            print('Failed and Retry')

#运行程序的顺序
if __name__ == '__main__':
    ip_list = get_ip_list(proxiesurl, headers=headers)
    proxies = get_random_ip(ip_list)
    mainpv()

#-*- MAX Young 2019-09-18  -*-

相关文章

  • python3 使用免费代理 IP 刷网站的访问量

    使用网络上免费的代理ip,让你的python浏览其它网站的时候不显示真实的ip,尤其是爬虫的时候,可以定时换ip。...

  • 这就是我的爬虫基本功!使用代理 IP和绕过反爬机制!

    使用代理 IP 之前我了解到的使用代理 IP 的方法,一般都是提前从一些免费的代理网站上爬取免费代理,存在本地或者...

  • python3代理使用

    代理IP网站:https://www.xicidaili.com(西刺免费代理)

  • 免费的换ip工具弊端

    从价格来看,代理ip有免费以及付费两种,关于免费代理ip的使用,狮子ip并不建议大家长期使用。 免费IP的提取还是...

  • Flask与Redis 维护代理池

    为什么要使用代理池? 许多网站反爬虫,会封IP 网上大量免费代理,利用好资源 定时的检测维护可以得到多个可用代理 ...

  • 简单测评拨号VPS——云立方&淘宝卖家

    做爬虫的同学不可避免地要使用代理IP,除了各网站公布的免费代理IP外,我们还可以选择拨号VPS,本文简单对两家(类...

  • 免费代理ip爬虫

    免费代理ip爬取(仅供参考!别干坏事哦) 使用Crawler4j开源工具爬取整个网站 快代理 西刺代理 89代理 ...

  • 代理池及squid的使用

    在爬虫的时候我们通常需要使用代理IP来避免被其他网站ban的情况,我们在其他网站上可以获取到免费的IP(成功率不高...

  • Python 学习记录1

    从豆瓣网下载整个相册的图片 从西祠代理网站爬取免费高匿ip 西祠代理 验证抓取的IP是否可用 建立代理IP池 抓...

  • python抓取代理IP并多线程验证

    一、抓取代理IP 提供免费代理IP的网站还挺多的,我在‘西刺代理’上一阵猛抓后自己的IP就被其屏蔽了。只好换‘IP...

网友评论

      本文标题:python3 使用免费代理 IP 刷网站的访问量

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