使用Python请求登录到Instagram [英] Logging to Instagram using Python requests

查看:220
本文介绍了使用Python请求登录到Instagram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用python编写脚本,登录到http://insta.friendorfollow.com/并获取未关注的人员列表.我想使用请求模块",到目前为止,我进行了很多尝试,但没有成功.我的代码如下:

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.

推荐答案

现在一切正常,

#!/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()

这篇关于使用Python请求登录到Instagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆