【问题标题】:Python Selenium not taking me to instagram (Bot)Python Selenium 没有带我去 Instagram(机器人)
【发布时间】: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


    【解决方案1】:

    嗯,看来您实际上并没有在任何地方开始上课。 在代码末尾添加:

    instagrambot = instabot()
    

    另外,在您的班级中,您没有运行任何功能。

    【讨论】:

    • 好的,谢谢你,我会在最后补充。如何运行这些功能? (对不起,如果这是一个愚蠢的问题)
    • 在您开始上课后,只需执行:instagrambot.functionname(whatever your parameters are) 即可获得所有不同的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2021-07-23
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2023-02-10
    相关资源
    最近更新 更多