python爬虫刷访问量 2019

    看着自己少得可怜的访问量,突然有一个想用爬虫刷访问量的想法,主要也是抱着尝试的心态,学习学习。

第一版:网上借鉴了一下           以下代码运行在 python3

import urllib.request
import time

# 使用build_opener()是为了让python程序模仿浏览器进行访问
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]


# 专刷某个页面
print('开始刷了哦:')
tempUrl = 'https://blog.csdn.net/Lin_QC/article/details/88966839'



for j in range(2000):
    try:
        opener.open(tempUrl)
        time.sleep(7)
        print('%d %s' % (j, tempUrl))
    except urllib.error.HTTPError:
        print('urllib.error.HTTPError')
        time.sleep(1)
    except urllib.error.URLError:
        print('urllib.error.URLError')
        time.sleep(1)

该代码主要就是利用爬虫打开网页来进行访问量的刷新,但是,该方法遇到了瓶颈,当刷新到一定访问量时,csdn的服务器会阻止该ip的访问,也就刷新不了访问量了。

所以,也就衍生了第二版。

我们可以在  https://www.xicidaili.com 网站上看到很多代理ip,使用这些代理ip,可以防止csdn服务器阻止访问。

首先,编写了一个获取代理ip的文件,经我本人实验,国内http代理ip较为稳定,所以我们爬取

'https://www.xicidaili.com/wt/1 

页面的代理ip信息,并将它们存储在proxy文件里,以下代码是基于 python2的,注意不要弄错版本

proxy_IP.py文件

import urllib2
import BeautifulSoup

User_Agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'
header = {}
header['User-Agent'] = User_Agent

url = 'https://www.xicidaili.com/wt/1'
req = urllib2.Request(url, headers=header)
res = urllib2.urlopen(req).read()

soup = BeautifulSoup.BeautifulSoup(res)
ips = soup.findAll('tr')
f = open("proxy", "w")

for x in range(1,len(ips)):
    ip = ips[x]

    tds = ip.findAll("td")
    ip_temp = tds[1].contents[0]+","+tds[2].contents[0]+"\n"

    print tds[1].contents[0]+"\t"+tds[2].contents[0]
    f.write(ip_temp)

通过执行以上代码,我们就可以获得大量代理ip,接下来就是使用这些ip进行对博客的访问。

csdnfake.py

import urllib2
import socket
import time
import random

socket.setdefaulttimeout(3)

user_agent_list = [
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3',
    'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
    'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
    'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
    'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
    'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
    'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
    'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)',
    'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0',
    'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
]
f = open("proxy")
lines = f.readlines()
proxys = []

for i in range(0,len(lines)):
    ip = lines[i].strip().split(",")
    proxy_host = "http://"+ip[0]+":"+ip[1]
    print "http://"+ip[0]+":"+ip[1]
    proxy_temp = {"http": proxy_host}
    proxys.append(proxy_temp)
urls = {"https://blog.csdn.net/Lin_QC/article/details/88966839",
       "https://blog.csdn.net/Lin_QC/article/details/88930018",
       "https://blog.csdn.net/Lin_QC/article/details/88642949",
       "https://blog.csdn.net/Lin_QC/article/details/84568170",
       "https://blog.csdn.net/Lin_QC/article/details/84451279",
       "https://blog.csdn.net/Lin_QC/article/details/84927503",
       }

j=1
for i in range(100):
    for proxy in proxys:
        for url in urls:
            try:
                user_agent = random.choice(user_agent_list)
                proxy_support = urllib2.ProxyHandler(proxy)
                opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
                urllib2.install_opener(opener)
                req = urllib2.Request(url)
                c = urllib2.urlopen(req)
                print ("sucessful",j)
                j+=1
                time.sleep(5)  
            except Exception, e:
                print proxy
                print e
                continue

user_agent_list是一堆浏览器的代理头,可以模仿浏览器访问博客。

每次访问休息五秒,主要是因为过快的访问对csdn无效。

效果,刷过访问量的博客和没刷的差距明显

其实,刷新访问量的行为是可耻的,本人也是被惨不忍睹的访问量激怒,好奇一试,之后还是会努力提高博客质量,靠实力提升访问量。

猜你喜欢

转载自blog.csdn.net/Lin_QC/article/details/89139208