Logging to Instagram using Python requests

一笑奈何 提交于 2019-12-31 10:53:30

问题


I am trying to write a script in python, to log into http://insta.friendorfollow.com/ and get the list of the people that are not following back. I want to use "requests module", so far I made numerous attempts with no luck. My code is as follow :

import requests, re

f = open('file', 'w')
r = requests.get('http://insta.friendorfollow.com/')
next_url = re.findall(ur'<a href=\"(.*)\" type=\"submit\"', r.content)

r = requests.get(next_url[0])

action = re.findall(ur'action=\"(.*)\"', r.content)
csrfmiddlewaretoken = re.findall(ur'name=\"csrfmiddlewaretoken\" value=\"(.*)\"', r.content)

print action
print csrfmiddlewaretoken

payload = {
'csrfmiddlewaretoken': csrfmiddlewaretoken[0],
'username': 'SOMEUSER',
'password':'SOMEPASS'
}

g = requests.post("https://instagram.com/"+action[0],
  data=payload, allow_redirects=True)


print >> f, g.text

Can some one tell me what I am doing wrong? and what would it be the right way to do it. A script would be much appreciated.


回答1:


All is fine now,

#!/usr/bin/env python

username = "username"
password = "password"

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re

driver = webdriver.PhantomJS()
driver.get("http://insta.friendorfollow.com")
driver.find_elements_by_tag_name("a")[1].click()

print "Perimene file....".upper()
driver.find_element_by_name('username').send_keys(username)
driver.find_element_by_name('password').send_keys(password)
driver.find_element_by_class_name("button-green").click()

try:
  driver.find_elements_by_name("allow")[1].click()
except:
  pass

f = open(username+".txt", 'w')
malakes = re.findall(ur'data-id=\"([0-9]*)\"', driver.page_source)[::-1]
for malakas in malakes:
  print >> f, malakas

f.close()
driver.quit()



回答2:


I had success in getting the list. I did a GET to http://insta.friendorfollow.com/following/ with appropriate authentication and I can see the users in the Response.

Here's an example of one of the users that are not following me (Floyd seems to not be interested in my posts)

style="margin:5px" alt="floydmayweather" title="floydmayweather" src="http://images.ak.instagram.com/profiles/profile_16264572_75sq_1394805311.jpg" data-id="16264572" data-action="unfollow"

and some other stuff.. So try adding /following to your URL



来源:https://stackoverflow.com/questions/22941347/logging-to-instagram-using-python-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!