python 增加博客访问

来源:互联网 发布:闻牛创富软件手机版 编辑:程序博客网 时间:2024/06/11 07:34

访问,还是真实的来的好~

做这个程序只是记录下学习的东西,基于python2

#!/usr/bin/env python# -*- coding:utf-8 -*-import  urllib2import random#构造请求头headers = {    "Accept": "application/json, text/javascript, */*; q=0.01",    "X-Requested-With": "XMLHttpRequest",    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",}#这里用了ProxyHandler 其实我们在调用urllib2.urlopen()时python调用的是 HTTPSHandler        https_handler = HTTPSHandler(context=context)        opener = build_opener(https_handler)        urllib2源码中可以看到proxy_handler = urllib2.ProxyHandler({"http":"113.200.159.155:9999"})opener = urllib2.build_opener(proxy_handler)#需要增加访问量的博客列表bloglist =  [    "http://blog.csdn.net/ashinli/article/details/76223063",    "http://blog.csdn.net/ashinli/article/details/76223063",    "http://blog.csdn.net/ashinli/article/details/75452023",    "http://blog.csdn.net/ashinli/article/details/74780958",    "http://blog.csdn.net/ashinli/article/details/76285460",]#下面就是主要实现功能的地方啦i = 1while(True):    #在博客列表中随机取一个url,其实博客列表可以做成先将自己的博客地址全部爬下来做成list    url = random.choice(bloglist)    request = urllib2.Request(url, headers=headers)    try:        print url+"增加访问1次"         #发送请求        response = opener.open(request)    except (Exception),e:        print  e        continue    i += 1
原创粉丝点击