github上的python爬虫_python爬虫入门(2):让你的github项目火起来
最近閑的比較無聊,于是想做一個自動star你的項目的爬蟲玩玩。不然star數太低了,也比較難看。思路是準備注冊成批的github帳號,然后挨個給你點star。
STEP.1 工具準備
我用的是python 2.7.10,本次實驗不需要下載依賴庫,用自帶的就行了。
import urllib2,urllib,re,time
STEP.2 如何準備github帳號
顯然,我們要star的話, 帳號少說也得有個1000個吧。1000個帳號手動注冊明顯是不可能的,所以我們得寫個小程序來注冊帳號。
我們首先來調研一下github注冊的過程。打開github-join
我們填入如下信息
用chrome開發者工具我們可以看到,數據以post的形式發給了這個鏈接'https://github.com/join'。
唯一還不明白的數據就是這個'authenticity_token'。但如果我們打開網頁的源碼,會發現每一頁都會有'authenticity_token',有時還有好幾個,而且還都不一樣。那我們該發的是哪一個'authenticity_token'呢。
事實上,github每一個可供post的按鈕都有token,你在post的時候,將離這個按鈕最近的token發過去就行了。
知道了這一點后,我們就能開始寫小程序了。
STEP.3 github_join的編寫
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上也是能登錄這個帳號的。說明我們成功了。下一步只需要批量生成帳號就行了。
STEP.4 登錄和點star
然后關于登錄和點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)
STEP.5 瘋狂star
這一步還能干啥呢,該干的都干了,現在當然是瘋狂的用剛剛注冊好的帳號來點star。我們來看看效果。
這是我的github頁面,很明顯能看到一堆機器在點贊。
不過我也是很怕被注意到然后被封掉的,所以我設置的延時比較長,點了一次贊之后,要過6秒才能點下一次。
這個項目po在這里githack。
我在** github.py 這個文件里寫了一個run函數,你只需要調用
run('你的id','你的倉庫名',需要點贊的次數)
就可以自動幫你點star了。
喜歡的點一波喜歡
總結
以上是生活随笔為你收集整理的github上的python爬虫_python爬虫入门(2):让你的github项目火起来的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux内核端口绑定,linux 多网
- 下一篇: websocket python爬虫_p