温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么利用python爬虫刷csdn文章的访问量

发布时间:2021-09-06 11:45:16 来源:亿速云 阅读:264 作者:chen 栏目:开发技术

这篇文章主要介绍“怎么利用python爬虫刷csdn文章的访问量”,在日常操作中,相信很多人在怎么利用python爬虫刷csdn文章的访问量问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么利用python爬虫刷csdn文章的访问量”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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

其实市面上有一些软件可以代刷流量 比如 流量精灵,使用感确实比我们自己写的代码要好一些

第一版:网上借鉴了一下           以下代码运行在 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无效。

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

到此,关于“怎么利用python爬虫刷csdn文章的访问量”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI