Created
July 19, 2015 16:06
-
-
Save 404neko/ec3505eb2c6277d78aec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
import json | |
Header={"Origin":"https://instagram.com" | |
,"Accept-Encoding":"gzip, deflate" | |
,"X-Instagram-AJAX":"1" | |
,"User-Agent":"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36" | |
,"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8" | |
,"Accept-Language":"en,zh-CN;q=0.8,zh;q=0.6" | |
,"Accept":"application/json, text/javascript, */*; q=0.01" | |
#,"Referer":"https://instagram.com/music/" | |
#,"X-CSRFToken":"9c1dd1821510342fceee93cc724c45be" | |
,"X-Requested-With":"XMLHttpRequest" | |
,"Connection":"keep-alive" | |
,"DNT":"1"} | |
PostData_0="q=ig_user(" | |
# 1791483929 | |
PostData_2=")+%7B+media.after(" | |
#1004385612385315349" | |
PostData_4="%2C+12)+%7B%0A++count%2C%0A++nodes+%7B%0A++++caption%2C%0A++++code%2C%0A++++comments+%7B%0A++++++count%0A++++%7D%2C%0A++++date%2C%0A++++display_src%2C%0A++++id%2C%0A++++is_video%2C%0A++++likes+%7B%0A++++++count%0A++++%7D%2C%0A++++owner+%7B%0A++++++id%0A++++%7D%0A++%7D%2C%0A++page_info%0A%7D%0A+%7D&ref=users%3A%3Ashow" | |
Proxies={'http':'http://2.3.3.3:23333' | |
,'https':'http://2.3.3.3:23333'}#此处应有代理 | |
def FormatPostData(PostData): | |
Dict={} | |
PostData=PostData.split('&') | |
for PostDataItem in PostData: | |
PostDataItem=PostDataItem.split('=') | |
Dict[PostDataItem[0]]=PostDataItem[1] | |
return Dict | |
''' | |
Session=requests.Session() | |
Request_G=Session.get('https://instagram.com/music/',proxies=Proxies) | |
Request_P=Session.post('https://instagram.com/query/',data=FormatPostData(PostData),proxies=Proxies) | |
print Session.cookies | |
''' | |
def GetAllFromInstagram(ID): | |
Session=requests.Session() | |
Aim='https://instagram.com/'+ID+'/' | |
S0=Session.get(Aim,headers=Header,proxies=Proxies) | |
S0_Soup=BeautifulSoup(S0.content) | |
#<script type="text/javascript" | |
ScriptJar=S0_Soup.findAll('script',{'type':'text/javascript'}) | |
Count=0 | |
MaxLength=0 | |
Max=0 | |
for Script in ScriptJar: | |
if len(Script.text)>MaxLength: | |
MaxLength=len(Script.text) | |
Max=Count | |
Count+=1 | |
else: | |
Count+=1 | |
Script=ScriptJar[Max].text | |
#EndTag=',"hostname"' | |
StartTag='indow._sharedData = ' | |
AimJson=Script.split(StartTag)[1][:-1] | |
Pjson=json.loads(AimJson) | |
X_CSRFToken=Pjson['config']['csrf_token'] | |
Header_Edited=Header | |
Header_Edited['Referer']=Aim | |
Header_Edited["X-CSRFToken"]=X_CSRFToken | |
UID=Pjson["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"][0]["owner"]["id"] | |
EndCursor=Pjson["entry_data"]["ProfilePage"][0]["user"]["media"]["page_info"]["end_cursor"] | |
PostData_=PostData_0+UID+PostData_2+EndCursor+PostData_4 | |
#PostData_=FormatPostData(PostData_) | |
#PostData_New={} | |
# for i in dict: | |
# print "dict[%s]=" % i,dict[i] | |
#for PostDataItem in PostData_: | |
# PostData_New[PostDataItem.encode('UTF-8')]=PostData_[PostDataItem].encode('UTF-8') | |
PostData_New=PostData_.encode('UTF-8') | |
S1=Session.post('https://instagram.com/query/',data=PostData_New,headers=Header_Edited,proxies=Proxies) | |
print S1.content | |
GetAllFromInstagram('music') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment