Python程序自动刷网站访问量

import requests
import time
import random
url = [‘http://cq.srx123.com/‘,
       ‘http://cq.srx123.com/article.php‘,
       ‘http://cq.srx123.com/yszc.php?act=k‘,
       ‘http://cq.srx123.com/download.php‘]

head = [‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36‘,
        ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0‘]

# DaiLi = [‘58.218.92.68:2360‘,‘58.218.92.72:6156‘,‘58.218.92.78:5424‘,
#‘58.218.92.74:4716‘,‘58.218.92.74:9387‘,‘58.218.92.78:2863‘,‘58.218.92.68:8890‘,‘58.218.92.77:2867‘,‘58.218.92.77:8749‘,
#‘58.218.92.73:7463‘,‘58.218.92.78:3749‘,‘58.218.92.68:9321‘,‘58.218.92.75:4647‘,‘58.218.92.73:6601‘,‘58.218.92.74:4077‘,
# ‘58.218.92.69:4815‘,‘58.218.92.68:3761‘,‘58.218.92.78:3447‘]



ShuLiang =1
for i in range(len(url)):
    for Tou in range(len(head)):
        headers = {"User-Agent": head[Tou]}//构建请求头

        # for Dai in range(len(DaiLi)):
        # proxies = {"http": "http://" + DaiLi[Dai]}//构建代理ip格式
        response = requests.get(url[i], headers=headers, timeout=10)//如果使用ip代理,get方法内需要添加对应参数
        if response.status_code == 200:
            print(‘第 ‘ + str(ShuLiang), ‘次访问成功,使用代理:‘ )
            ShuLiang += 1
            DengDai = random.randint(0, 99)
            print(DengDai)
            time.sleep(DengDai)
        else:
            print("访问失败")
            ShuLiang += 1

最近学校因为专业课的问题,老师给我们布置了一个网站运营的作业,考核标准就是网站的访问量。所以我便用Python写了这样一个程序(部分代码)

程序能用但是还是存在一些问题,比如访问过快的话会被服务器当作是DDOS攻击屏蔽掉,或者说访问速度过快被统计端屏蔽掉等等。

个人建议:使用时应当注意目标网站是否允许网络爬虫的访问,还有就是应当注意网络爬虫使用的道德规范,可以通过查看目标网站对爬虫的限制来进行特定的访问爬取

相关推荐