最近闲的比较无聊,于是想做一个自动star你的项目的爬虫玩玩。不然star数太低了,也比较难看。思路是准备注册成批的github帐号,然后挨个给你点star。
我用的是python 2.7.10,本次实验不需要下载依赖库,用自带的就行了。
import urllib2,urllib,re,time
显然,我们要star的话, 帐号少说也得有个1000个吧。1000个帐号手动注册明显是不可能的,所以我们得写个小程序来注册帐号。
我们首先来调研一下github注册的过程。打开github-join
我们填入如下信息

用chrome开发者工具我们可以看到,数据以post的形式发给了这个链接\’https://github.com/join\’。

唯一还不明白的数据就是这个\’authenticity_token\’。但如果我们打开网页的源码,会发现每一页都会有\’authenticity_token\’,有时还有好几个,而且还都不一样。那我们该发的是哪一个\’authenticity_token\’呢。
事实上,github每一个可供post的按钮都有token,你在post的时候,将离这个按钮最近的token发过去就行了。
知道了这一点后,我们就能开始写小程序了。
class gitjoin:
def __init__(self):
cookies = urllib2.HTTPCookieProcessor() #构造cookies
self.opener = urllib2.build_opener(cookies)
self.opener.addheaders = [(\'User-agent\', \'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36\'),(\'Origin\',\'https://github.com\'
),(\'Accept\',\'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\'),(\'Accept-Language\',\'zh-CN,zh;q=0.8\'),
(\'Connection\',\'keep-alive\')] #请求头
self.re_auth = re.compile(\'authenticity_token[^>]+\') #得到github token信息
我们用一个叫gitjoin的类来封装我们的注册机。这里我们的正则表达式只用了一次,只要匹配那个\’token\’就行了。
然后我们需要获得token。这个token就是用来注册的。代码如下。
def view(self):
response = self.opener.open(\'https://github.com/join\')
html = response.read()
print u\'正在登录join\'
print u\'状态码为\',response.getcode()
token = self.re_auth.findall(html)[0][41:-3]
return token
获得了token之后,我们就可以开始注册了。
def zhuce(self,token,login,email,password):
self.formdata = {\'utf-8\':\'✓\',\'authenticity_token\':token,\'user[login]\':login,\'user[email]\':email,\'user[password]\':password,\'source\':\'form-home\'}
data_encoded = urllib.urlencode(self.formdata)
print data_encoded
response = self.opener.open(\'https://github.com/join\',data_encoded)
print u\'正在注册\'
print u\'状态码为\',response.getcode(),u\'转到\',response.geturl()
注册完了,就结束了吗?
很明显没这么简单,你注册完之后,你的cookies保存了你的用户信息,你这时是不能够继续注册的。你需要logout之后,才能注册。
这里logout我就不用chrome分析了。反正就是post一个数据给https://github.com/logout,不过要记得加token(注意这里的token不是刚刚的注册token,我们需要重新获取它的logout token)。我直接上代码吧。
这是获取logout token
def view_index(self):
response = self.opener.open(\'https://github.com/\')
html = response.read()
token = self.re_auth.findall(html)[0][41:-3]
return token
这是logout
def logout(self,token):
self.formdata = {\'utf-8\':\'✓\',\'authenticity_token\':token}
data_encoded = urllib.urlencode(self.formdata)
response = self.opener.open(\'https://github.com/logout\',data_encoded)
print u\'正在登出\'
print u\'状态码为\',response.getcode(),u\'转到\',response.geturl()
现在gitjoin类就已经写好了,我们可以尝试注册一个账号了。
signin = gitjoin()
token = signin.view()
signin.zhuce(token,\"pighasa100\",\"pighasa100@qq.com\",\"pighasa1\")
执行代码后,发现在github上也是能登录这个帐号的。说明我们成功了。下一步只需要批量生成帐号就行了。
然后关于登录和点star的问题。之前我在叙述如何注册的时候就说过了,这里我就简要概括了。你们可以自己用chrome或者火狐或者wireshark去分析,我这里就直接上代码了。
这里是login
def login(self,token,usr,password):
self.formdata = {\'commit\':\'Sign in\',\'utf-8\':\'✓\',\'authenticity_token\':token,\'login\':usr,\'password\':password}
data_encoded = urllib.urlencode(self.formdata)
response = self.opener.open(\'https://github.com/session\',data_encoded)
print u\'现在正在登陆\',usr
这里是star
def star(self,usrName,repoName):
url = \'\'.join([\'https://github.com/\',usrName,\'/\',repoName,\'/\'])
response = self.opener.open(url)
html = response.read()
token = self.re_auth.findall(html)[3][41:-3]
formdata = {\'utf-8\':\'✓\',\'authenticity_token\':token}
data_encoded = urllib.urlencode(formdata)
response = self.opener.open(url+\'star\',data_encoded)
这一步还能干啥呢,该干的都干了,现在当然是疯狂的用刚刚注册好的帐号来点star。我们来看看效果。
这是我的github页面,很明显能看到一堆机器在点赞。

不过我也是很怕被注意到然后被封掉的,所以我设置的延时比较长,点了一次赞之后,要过6秒才能点下一次。
这个项目po在这里githack。
我在 github.py 这个文件里写了一个run函数,你只需要调用
run(\'你的id\',\'你的仓库名\',需要点赞的次数)
就可以自动帮你点star了。
喜欢的点一波喜欢
下一篇:Django学习笔记