python爬虫增加访问量的方法

yipeiwu_com6年前Python爬虫

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

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

第一版:网上借鉴了一下           以下代码运行在 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爬虫增加访问量的方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

Python发展史及网络爬虫

Python 简介 Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。 Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些...

python制作爬虫爬取京东商品评论教程

python制作爬虫爬取京东商品评论教程

本篇文章是python爬虫系列的第三篇,介绍如何抓取京东商城商品评论信息,并对这些评论信息进行分析和可视化。下面是要抓取的商品信息,一款女士文胸。这个商品共有红色,黑色和肤色三种颜色,...

详解Python爬虫的基本写法

什么是爬虫 爬虫,即网络爬虫,大家可以理解为在网络上爬行的一直蜘蛛,互联网就比作一张大网,而爬虫便是在这张网上爬来爬去的蜘蛛咯,如果它遇到资源,那么它就会抓取下来。想抓取什么?这个由你来...

python爬虫爬取快手视频多线程下载功能

python爬虫爬取快手视频多线程下载功能

环境: python 2.7 + win10 工具:fiddler postman 安卓模拟器 首先,打开fiddler,fiddler作为http/https 抓包神器,这里就不多介绍...

Python爬取国外天气预报网站的方法

本文实例讲述了Python爬取国外天气预报网站的方法。分享给大家供大家参考。具体如下: crawl_weather.py如下: #encoding=utf-8 import http...