python - 无法在某些搜索框中使用主题标签生成结果

标签 python python-3.x selenium selenium-webdriver web-scraping

我使用 Selenium 在 Python 中编写了一个脚本来登录 Instagram,然后搜索一些主题标签,如 #NewYorkbarbers,并获取该主题标签的链接。我的脚本可以成功登录,如果出现 Turn on Notifications 框,请单击 Not Now 按钮,然后将该主题标签放入搜索框中,但我不能让我的脚本启动该搜索以针对该主题标签生成结果。

到目前为止我已经尝试过:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class InstagramScraper:

    login_url = 'https://www.instagram.com/accounts/login/?source=auth_switcher'

    def __init__(self,username,password):
        self.driver = webdriver.Chrome()
        self.wait = WebDriverWait(self.driver,10)
        self.login(username,password)

    def login(self,username,password):
        self.driver.get(self.login_url)
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[name="username"]'))).send_keys(username)
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[name="password"]'))).send_keys(password)
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'button[type="submit"]'))).click()
        try:
            self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'button.HoLwm'))).click()
        except Exception:pass

    def use_hashtag(self):
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[placeholder="Search"]'))).send_keys("#NewYorkbarbers",Keys.ENTER)

if __name__ == '__main__':
    scraper = InstagramScraper('username','password')
    scraper.use_hashtag()
  • 我如何在 login() 方法中使用 return,因为任何理想的方法都应该在其中包含 return 语句?

  • 如何生成该主题标签搜索的结果?

最佳答案

有返回语句的一般想法是返回。 当您在声明中返回时,它会返回到调用它的地方。 当你写 return value 时,它意味着返回并获取这个值。 如果没有给出 return 语句,则表示 return None

关于python - 无法在某些搜索框中使用主题标签生成结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57017486/

相关文章:

java - 如何使用 FirefoxDriver 对象使用 waitForCondition() 函数?

python - sudo cd 进入带有 Fabric 的目录

python - Python read()在readlines()工作时引发MemoryError

python-3.x - 在 Python 中的置换树上实现深度优先搜索

Python 3 - 我是否正确使用了索引?

javascript - Python Selenium AWS Lambda 更改 WebGL vendor/渲染器以实现无法检测的 headless 刮刀

java - lang.NoClassDefFoundError : org/apache/http/impl/conn/PoolingClientConnectionManager htmlunit. HttpWebConnection.createHttpClient

python - 如何检查两个列表中的元素是否相同(顺序已更改)

python - 安装 CPLEX 12.6 for Python 时出错

python-3.x - 如何计算一系列特定的、恒定宽度的列范围的平均值?