背景:
我已经尝试解决了一段时间。我正在使用Selenium(chrome网络驱动程序)从桌面访问Instagram的移动版本。我的目标是创建一个脚本来自动将图像发布到帐户。使用Python,我可以自动完成所有需要选择文件的部分。
问题:
一旦我单击“创建新帖子”按钮,gui文件选择器就会打开,并且我不知道如何与之交互以选择文件。
尝试:
post_btn.send_keys(r'/ Path / To / image.jpg')
post_btn.send_keys('/ Path / To / image.jpg')
我认为使用此方法的麻烦在于没有输入字段可将字符串发送到。
我还阅读了一些其他有关使用autoit并能正常工作的文章,但我使用的是Mac。
以下是发布按钮所在的范围:
<span class="glyphsSpriteNew_post__outline__24__grey_9 u-__7" aria-label="New Post"></span>
问题:
有没有办法使用python通过文件选择器gui“打开”(上传)文件? 更好,我可以完全绕开打开文件选择器吗?
谢谢!
编辑:
我认为阻止我解决此问题的原因是我不了解如何发送和接收数据。由于它不是html输入,是否表示它是JSON?如何确定数据(图像)如何发送到下一页(https://www.instagram.com/create/style/)?
答案 0 :(得分:2)
IDK是否对您有用,因为它已经存在很长时间了。但是对任何需要它的人来说,都是完整的教程,
进口:
import os
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import autoit
import time
from selenium.webdriver.common.keys import Keys
因此,首先您需要一个模拟器。 我发现的最佳方法是使用chromedriver.exe,如果您使用chrome作为浏览器,则已经需要。因此,要创建仿真器,您需要驱动程序的以下代码:
mobile_emulation = {
"deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options = chrome_options)
然后您登录:
driver.get('https://www.instagram.com/accounts/login/')
time.sleep(2)
driver.find_element_by_name("username").send_keys("Your Username")
driver.find_element_by_name("password").send_keys("Your Password")
driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/article/div/div/div/form/div[7]/button""").click()
time.sleep(2)
driver.get('https://www.instagram.com/' + username)
命名您的文件(必须是全名):
ImagePath = 'Your File Location'
最后一步是这个
ActionChains(driver).move_to_element( driver.find_element_by_xpath("""//*[@id="react-root"]/section/nav[2]/div/div/div[2]/div/div/div[3]""")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 3)
autoit.control_set_text(handle, "Edit1", dir_path)
autoit.control_click(handle, "Button1")
time.sleep(2)
driver.find_element_by_xpath("""//*[@id="react-root"]/section/div[1]/header/div/div[2]/button""").click()
time.sleep(2)
txt = driver.find_element_by_class_name('_472V_')
txt.send_keys('')
txt = driver.find_element_by_class_name('_472V_')
txt.send_keys('test') # Descrition
txt.send_keys(Keys.ENTER)
driver.find_element_by_xpath("""//*[@id="react-root"]/section/div[1]/header/div/div[2]/button""").click()
所有这些操作就是转到上传页面,并使用autoit在窗口中导航以选择文件并选择。然后只需添加说明并分享帖子即可。
答案 1 :(得分:0)
我在最后尝试了此方法,可以清楚地看到没有办法绕过文件选择器。话虽如此,您需要使用外部工具来自动执行文件选择器。以我的经验,Sikuli可能最适合这份工作。
答案 2 :(得分:0)
您可以使用 Instagram API 上传图片/视频。
安装 pip install InstagramApi
from InstagramAPI import InstagramAPI
api = InstagramAPI("username", "password")
if (api.login()):
api.getSelfUserFeed() # get self user feed
print(api.LastJson) # print last response JSON
print("Login succes!")
else:
print("Can't login!")
测试登录
from InstagramAPI import InstagramAPI
InstagramAPI = InstagramAPI("login", "password")
InstagramAPI.login() # login
photo_path = '/path/to/photo.jpg'
caption = "Sample photo"
InstagramAPI.uploadPhoto(photo_path, caption=caption)
上传带有标题的图像:
from InstagramAPI import InstagramAPI
import urllib
video_url = 'https://instagram.fmad3-2.fna.fbcdn.net/t50.2886-16/17157217_1660580944235536_866261046376005632_n.mp4' # a valid instagram video
video_local_path = video_url.split("/")[-1]
thumbnail_url = "https://instagram.fmad3-2.fna.fbcdn.net/t51.2885-15/e15/17075853_1759410394387536_3927726791665385472_n.jpg"
thumbnail_local_path = thumbnail_url.split("/")[-1]
urllib.urlretrieve(video_url, video_local_path)
urllib.urlretrieve(thumbnail_url, thumbnail_local_path)
InstagramAPI = InstagramAPI("login", "password")
InstagramAPI.login() # login
InstagramAPI.uploadVideo(video_local_path, thumbnail_local_path, caption="Tortuguero")
上传视频:
git clone https://github.com/LevPasha/Instagram-API-python.git
(可选)下载具有良好示例的github存储库。
cd Instagram-API-python/examples
打开“示例”文件夹
login
编辑文件:用用户名和密码替换password
和apparently
。
运行文件
答案 3 :(得分:0)
我在某个地方找到了它,并且在python中工作正常。 导入pyautogui pyautogui.write('D:\ opt \ FitnessStudio \ picofme.jpg') pyautogui.press('enter')
答案 4 :(得分:0)
# ------------ Imports ---------------
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import random
# ------------ Def Random --------------
def sleepTime(start):
_sleep = (random.randint(start, 20))
time.sleep(_sleep)
# ---------------- WebDriver -----------------
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.instagram.com/accounts/login/')
driver.email = '*instaID*'
driver.password = '*instapswd*'
driver.username = '*instapageusernameyouwanttofollow*'
sleepTime(5)
try:
emailInput=driver.find_element_by_xpath("//input[@name = 'username']")
passwordInput=driver.find_element_by_xpath("//input[@name = 'password']")
except (NoSuchElementException, ElementNotVisibleException) as exceptions:
pass
emailInput.send_keys(driver.email)
passwordInput.send_keys(driver.password)
passwordInput.send_keys(Keys.ENTER)
sleepTime(7)
# ---pop up handle---
NotNow=driver.find_element_by_xpath("//button[text() = 'Not Now']").click()
sleepTime(5)
# ---------- Going to folower list ----------
driver.get('https://www.instagram.com/' + driver.username + '/')
sleepTime(7)
driver.get('https://www.instagram.com/' + driver.username + '/followers/')
Followers=driver.find_element_by_xpath("//a[@class='-nal3 ']").click()
sleepTime(7)
# -------------------------------------------
#find all li elements in list
fBody = driver.find_element_by_xpath("//div[@class='isgrP']")
scroll = 0
for j in (1,(random.randint(4, 20))):
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
sleepTime(10)
# for k in (1,4):
# driver.execute_script('arguments[0].scrollDown = arguments[0].scrollDown + arguments[0].offsetHeight;', fBody)
# time.sleep(10)
count = 0
while scroll < 500: # scroll 500 times
sleepTime(10)
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
for i in (0,(random.randint(5, 20))):
try:
Follow=driver.find_element_by_xpath("//button[text() = 'Follow']")
Follow.click()
print(count)
count = count + 1
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
time.sleep(10)
except NoSuchElementException:
pass
sleepTime(8)
# driver.executeScript("window.scrollBy(0,100)")
scroll += 1
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
sleepTime(4)
driver.close()