【发布时间】:2021-10-12 12:44:25
【问题描述】:
我对此真的很陌生,所以它可能非常简单,但这段代码甚至不会被带到 instagram(我已经开始搜索主题标签,但后来我按照教程将它们放入 defs 我是观看)。我认为我使用 def 函数的方式有些错误,这就是为什么它不会去任何地方。
from selenium import webdriver
import random
PATH = "C:\Program Files (x86)\chromedriver.exe"
from time import sleep
class instabot():
def login(self, username, password):
self.driver = webdriver.Chrome(PATH)
self.driver.get('https://www.instagram.com')
self.driver.find_element_by_xpath('/html/body/div[4]/div/div/button[1]').click()
sleep(random.randint(3,10))
self.driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input').send_keys(username)
sleep(random.randint(3,10))
self.driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input').send_keys(password)
sleep(random.randint(3,10))
self.driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]/button/div').click()
sleep(random.randint(3,10))
self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/div/div/button').click()
sleep(random.randint(3,10))
#driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[3]/button[2]').click()
#sleep(random.randint(3,10))
def like_comment_hashtag(self, hashtags):
driver = webdriver.Chrome(PATH)
driver.get("https://www.instagram.com/explore/" + random.choice(hashtags))
sleep(random.randint(3,6))
links = self.driver.find_elements_by_tag_name('a')
def condition(link):
return '.com/p/' in link.get_attribute('href')
valid_links = list(filter(condition, links))
for i in range(5):
link = valid_links[i].get_attribute('href')
if link not in self.links:
self.links.append(link)
for link in self.links:
self.driver.get(link)
sleep(random.randint(3,10))
self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div[1]/article/div[3]/section[1]/span[1]/button').click()
sleep(5)
#comment
self.driver.find_element_by_class_name('RxpZH').click()
sleep(random.randint(3,10))
self.driver.find_element_by_xpath("//textarea[@placeholder='Add a comment…']").send_keys(random.choice(comments))
sleep(random.randint(3,10))
self.driver.find_element_by_xpath("//button[@type='submit']").click()
【问题讨论】:
标签: python selenium-webdriver automation bots instagram