你们!
[我正在通过修改其他人的脚本来学习Python,我的第一个迷你项目是一个Instagram机器人,它使用适用于Chrome的Selenium webdrive来评论和喜欢基于标签的帖子。这张单据是加里·维(Gary Vee)的作品,但我无法像上班一样(评论很棒!)。我使用Pyhton 3.8来运行它。
脚本是:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import random
import sys
import time
class GaryVee:
username = 'xxxxxx'
password = 'yyyyyy'
hashtags = [
'zzzzzz'
]
comments = [
'tttttttt'
]
links = []
price = 0.0
def __init__(self):
self.browser = webdriver.Chrome()
self.login()
self.hustle()
def login(self):
self.browser.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
time.sleep(2)
username_field = self.browser.find_element_by_xpath("//input[@name='username']")
username_field.clear()
username_field.send_keys(self.username)
time.sleep(1)
password_field = self.browser.find_element_by_xpath("//input[@name='password']")
password_field.clear()
password_field.send_keys(self.password)
time.sleep(1)
login_button = self.browser.find_element_by_xpath("//button[@type='submit']")
login_button.click()
time.sleep(2)
def hustle(self):
self.getTopPosts()
self.execute()
self.finalize()
def getTopPosts(self):
for hashtag in self.hashtags:
self.browser.get('https://www.instagram.com/explore/tags/' + hashtag +'/')
time.sleep(2)
links = self.browser.find_elements_by_tag_name('a')
condition = lambda link: '.com/p/' in link.get_attribute('href')
valid_links = list(filter(condition, links))
for i in range(0, 9):
link = valid_links[i].get_attribute('href')
if link not in self.links:
self.links.append(link)
def execute(self):
for link in self.links:
self.browser.get(link)
time.sleep(1)
self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1)
self.comment()
time.sleep(2)
self.like()
#self.price += 0.02
#sleeptime = random.randint(18, 28)
#time.sleep(sleeptime)
def comment(self):
comment_input = lambda: self.browser.find_element_by_tag_name('textarea')
comment_input().click()
comment_input().clear()
comment = random.choice(self.comments)
for letter in comment:
comment_input().send_keys(letter)
delay = random.randint(1, 7) / 30
time.sleep(delay)
comment_input().send_keys(Keys.RETURN)
def like(self):
like_button = lambda: self.browser.find_element_by_xpath('/html/body/div[3]/div[2]/div/article/div[2]/section[1]/span[1]/button/span')
like_button().click()
def finalize(self):
print('You gave $' + str(self.price) + ' back to the community.')
self.browser.close()
sys.exit()
garyVee = GaryVee()
这是我在命令行中得到的:
DevTools listening on ws://127.0.0.1:51688/devtools/browser/c78e1107-b44d-4758-8028-06b0e9c8f809
Traceback (most recent call last):
File "D:\Dropbox\Python\instagram-bot.py", line 104, in <module>
garyVee = GaryVee()
File "D:\Dropbox\Python\instagram-bot.py", line 26, in __init__
self.hustle()
File "D:\Dropbox\Python\instagram-bot.py", line 48, in hustle
self.execute()
File "D:\Dropbox\Python\instagram-bot.py", line 76, in execute
self.like()
File "D:\Dropbox\Python\instagram-bot.py", line 97, in like
like_button().click()
File "D:\Dropbox\Python\instagram-bot.py", line 96, in <lambda>
like_button = lambda: self.browser.find_element_by_xpath('/html/body/div[3]/div[2]/div/article/div[2]/section[1]/span[1]/button/span')
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[2]/div/article/div[2]/section[1]/span[1]/button/span"}
(Session info: chrome=80.0.3987.87)
我想我应该在like(self)中更改find_element函数(或其参数),但是即使在网上找到了很多示例之后,我也无法弄清楚那里有什么...
任何帮助将不胜感激!
谢谢,阿农
让我知道您是否知道这一点。我在同一个问题上苦苦挣扎。
sn:instagram是否可以检测硒?这里是关于它的讨论,但是我有点困惑,谢谢: