python脚本:访问网易博客实例

决定上半年抽业余时间学习Python,前一周把网上的《A Byte of Python》打印出来看了一篇,基本了解Python的语法等基础知识。
这里show一个访问网易博客的python脚本吧,这是去年为了好玩在网上找的原型,去年已经可以在python2.6上work了的,现在在学习的是 python3了,所以下面的是python3的代码,另外加上公司的网络都要设置代理才能访问外网,所以添加了代理的设置。
BTW,经过测试,该脚本可以作为网易博客的顶贴器,貌似网易博客没有对同一个IP的多次访问记录做去重之类的处理。当然除了简单的测试之外,我并没有用 这种程序来刷我的博客记录,我觉得这样也没意思。代码中visitTimesPerPage变量设置的是访问每个url的次数,要是设为1000次,则每 个url都会访问1000遍。
代码如下:

#! /usr/bin/python3# -*- coding: utf-8 -*-
”’Created on 2011-04-06@author: 笑遍世界”’
import sysimport threadingimport urllib.request
urls = ["blog/static/16005312010101782448207/","blog/static/16005312010111233057624/","blog/static/16005312010111244548449/",]
visitTimesPerPage = 10
def usage(): print(‘Usage:’,sys.argv[0],‘host’)
def main(argv): host = argv[1] if host == ”: usage() sys.exit(2) else: for i in range(visitTimesPerPage): for url in urls: visitPageThread = VisitPageThread(url + str(i),host,url) visitPageThread.start()

class VisitPageThread(threading.Thread): def __init__(self,threadName,url): threading.Thread.__init__(self,name = threadName) self.host = host self.url = url def run(self): url = self.host + self.url req = urllib.request.Request(url) req.set_proxy(‘companyname.com:911′,‘http’)
#you may set you proxy here.
try: doc = urllib.request.urlopen(req).read() print(doc) except Exception as e: print(“urlopen Exception : %s” %e)

if __name__==’__main__’: sys.argv.append(‘http://ren**.blog.163.com/’) main(sys.argv)


这也是我贴的第一个python脚本哦,mark一下!~


相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...