用python制作一个instagram发布机器人?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31938658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:46:20  来源:igfitidea点击:

Making an instagram posting bot with python?

pythoninstagram

提问by programminglearner

Ok so I know nothing about programming with python yet but I have wanted to make a bot to post to instagram for a while so thought it would be a good way to 'hit the ground running'.

好的,所以我对使用 python 编程一无所知,但我一直想制作一个机器人来发布到 Instagram 一段时间,所以我认为这将是“开始运行”的好方法。

I dont have a specific time frame so no rush.

我没有特定的时间框架,所以不要着急。

I don't know any programming languages yet but have wanted to branch out since I use a GUI based web automaiton tool which I see has quite alot of overlap with programming languages such as if statements, variables, loops etc.

我还不知道任何编程语言,但我想扩展,因为我使用基于 GUI 的 Web 自动化工具,我发现它与编程语言有很多重叠,例如 if 语句、变量、循环等。

I have been feeling that learning a proper language will be a better investment long term.

我一直觉得学习一门正确的语言将是更好的长期投资。

So since I know nothing about it, but I have my goal in mind can people suggest what where I start in terms of what I should study for the task? Then I can laser focus what I need to learn and work at it piece by piece.

因此,既然我对此一无所知,但我心中有我的目标,人们能否就我应该为任务学习的内容提出建议?然后我可以激光聚焦我需要学习的内容并一点一点地工作。

I want to just upload pictures as one operation and follow/unfollow as another on instagram. So please illuminate me on how Id go about that. I was told that python is the best all rounder to learn since it does everything in a tidy fashion ie less code and is intuitive. I will want to make other projects in future based on web automation so felt this would be a good one to learn from what I was told by a pro programmer.

我只想将图片作为一项操作上传,然后在 Instagram 上作为另一项操作关注/取消关注。所以请让我了解 Id 如何做到这一点。有人告诉我,python 是最好的全能型学习者,因为它以整洁的方式完成所有工作,即代码更少且直观。我将来会想基于 Web 自动化制作其他项目,所以我觉得这将是一个很好的学习专业程序员告诉我的项目。

I understand I may have been vague but not sure what to ask yet given my ignorance so please ask away if needed to hone the question/s.

我知道我可能含糊不清,但鉴于我的无知,我不确定该问什么,所以请询问是否需要完善问题。

回答by Eloims

With Instagram there is no need to do "web automation", because there is an API that you can simply work with.

使用 Instagram 无需进行“网络自动化”,因为您可以轻松使用 API。

It's even easier than this, there is a python library ready to use: https://github.com/Instagram/python-instagram

比这更简单,有一个可以使用的python库:https: //github.com/Instagram/python-instagram

As a sidepoint, I cannot resist to comment your sentence "I was told that python is the best all rounder to learn since it does everything in a tidy fashion ie less code and is intuitive.".

顺便说一句,我忍不住评论你的句子“有人告诉我python是最好的全能者,因为它以整洁的方式完成所有事情,即更少的代码并且很直观。”。

Python is indeed a good language to learn, but all languages have theirs pros and cons, so try to forget the idea that "there is a best language". If that was the case, we would all use the same one and not bother learning many of them :)

Python确实是一门好学的语言,但所有语言都有其优点和缺点,所以尽量忘记“有一种最好的语言”的想法。如果是这样的话,我们都会使用相同的一个,而不用费心去学习其中的很多 :)

回答by Joachim

I think if you have never programmed before the Instagram thing might not be a good first project. It is a good motivational mid-term goal though.

我认为如果你之前从未编程过 Instagram 的东西可能不是一个好的第一个项目。不过,这是一个很好的激励性中期目标。

Check out interactive tutorials like https://www.codecademy.com/tracks/python

查看交互式教程,如https://www.codecademy.com/tracks/python

回答by DJanssens

Do you have some initial programming experience already? If not, take a quick introduction into the world of python. There exist tons of good information sources/tutorialsalready.

您是否已经有一些初步的编程经验?如果没有,请快速了解一下 Python 的世界。已经存在大量好的信息源/教程

In order to communicate with Instagram you will be interacting with the Instagram API. This is a set of functions that are provided by Instagram and that allow you to interact with their data. These interactions occur with http requests and they are often formatted in json.

为了与 Instagram 交流,您将与Instagram API进行交互。这是一组由 Instagram 提供的功能,允许您与他们的数据进行交互。这些交互发生在 http 请求中,它们通常采用 json 格式。

Luckily in python there exists a library that already encapsulates most of these API interactions, which can be found here. This way you do not have to worry about the http requests part, which I believe can be quite hard for a beginner. If you feel a little comfortable with python, you should go through the read-me file of the github project; it should give you a basic idea of how to get started.

幸运的是,在 python 中存在一个已经封装了大多数这些 API 交互的库,可以在这里找到。这样你就不必担心 http 请求部分,我相信这对初学者来说可能很难。如果你觉得python有点舒服,你应该阅读github项目的read-me文件;它应该让您对如何开始有一个基本的了解。

回答by AndyRyan

You should note that while you can follow and unfollow users and like and unlike media. you CAN NOT post to Instagram using their API.

您应该注意,虽然您可以关注和取消关注用户以及喜欢和不喜欢媒体。你不能使用他们的 API 发布到 Instagram。

回答by Pravin Nath

So since I know nothing about it, but I have my goal in mind can people suggest what where I start in terms of what I should study for the task? Then I can laser focus what I need to learn and work at it piece by piece.

因此,既然我对此一无所知,但我心中有我的目标,人们能否就我应该为任务学习的内容提出建议?然后我可以激光聚焦我需要学习的内容并一点一点地工作。

Hard to answer. but I will try my best.

很难回答。但我会尽力而为。

On Github there's a great instagram bot project. you can try your hands on. But as you try you will realize that you can't understand the project without learning the basic concepts of python.

在 Github 上有一个很棒的instagram 机器人项目。你可以试试你的手。但是当你尝试的时候你会发现,如果不学习python的基本概念,你是无法理解这个项目的。

So, start with Sololearn. Similar to Stack overflow, Sololearn has a huge community. (unlike stack overflow sololearn's community is friendly)

所以,从Sololearn开始。与 Stack Overflow 类似,Sololearn 拥有庞大的社区。(不像堆栈溢出 sololearn 的社区是友好的)

回答by Glyphack

I have used this api for posting on instagram https://github.com/LevPasha/Instagram-API-pythonHere's the code:

我已经使用这个 api 在 instagram 上发帖 https://github.com/LevPasha/Instagram-API-python下面是代码:

from InstagramAPI import InstagramAPI
InstagramAPI = InstagramAPI(user,pwd)
InstagramAPI.login() # login
photo_path ='path to image'
caption = "your caption"
InstagramAPI.uploadPhoto(photo_path, caption = caption)

回答by Ivan

It's a bit heavy, but you can use Selenium and do a bot on your browser. You can even make automatic clicks on the window if you don't want to read web codes.

它有点重,但您可以使用 Selenium 并在您的浏览器上做一个机器人。如果您不想阅读网络代码,您甚至可以自动点击窗口。

回答by Jean Pimenta

A idea is to use Selenium, webdriverand chromedriverto open a Chrome tab, login into the account and do some job like click in some place, do something, etc. Example:

一个想法是使用Seleniumwebdriverchromedriver打开 Chrome 选项卡,登录帐户并执行一些操作,例如单击某个地方、执行某些操作等。例如:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
import pandas as pd

optionss = webdriver.ChromeOptions()
optionss.binary_location = 'C:/Program Files (x86)/Google/Chrome Beta/Application/chrome.exe' # Directory where your chrome.exe is
chrome_driver_binary = './chromedriver.exe' # Change this to your own chromedriver path! 
webdriver = webdriver.Chrome(executable_path=chrome_driver_binary, options=optionss)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)

username = webdriver.find_element_by_name('username')
username.send_keys('YOUR-USERNAME')
password = webdriver.find_element_by_name('password')
password.send_keys('YOUR-PASS')

button_login = webdriver.find_element_by_xpath('//button[normalize-space()="Login"]')
button_login.click()
sleep(4)

notnow = webdriver.find_element_by_xpath('//button[normalize-space()="Not now"]')
notnow.click() #comment these last 2 lines out, if you don't get a pop up asking about notifications
sleep(2)

##############
### From here, put the tasks you want to be done,
### like click to send image, etc, etc. 
### You need to study the html code.
##############

Depending on the task to be performed, put some sleep()between one and the other to avoid having your account to be suspended for some time.

根据要执行的任务,在一个和另一个之间放置一些sleep()以避免您的帐户被暂停一段时间。

To get those XPath, I use Opera browser. Right click > Inspect element > Find the html code> Right click on this html code > Copy > Copy XPath or Full XPath

为了获得这些XPath,我使用 Opera 浏览器。右键单击> 检查元素>查找html 代码> 右键单击​​此html 代码> 复制> 复制XPath 或完整XPath

And use this into the find_element_by_xpath. Have many ways to do this... choose yours.

并将其用于find_element_by_xpath。有很多方法可以做到这一点......选择你的。