【问题标题】:Gather Insagram Username using selenium python使用 selenium python 收集 Insagram 用户名
【发布时间】:2021-03-13 21:29:47
【问题描述】:

我正在尝试保存在 Instagram 中喜欢我帖子的用户名

这是我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from time import *
chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data")
browser = webdriver.Chrome(options=chrome_options)

browser.get('https://www.instagram.com/p/CMAVjR5CmOx/')


continue_link = browser.find_element_by_partial_link_text('others')
continue_link.click()

elems = browser.find_elements_by_css_selector(".FPmhX.notranslate.MBL3Z")
links = [elem.get_attribute('title') for elem in elems]

WebElement = browser.find_element_by_xpath('//*[ contains (text(), ‘#’ ) ]')
WebElement.click()

browser.execute_script("arguments[0].scrollIntoView();", WebElement)

问题是,它只保存了 11 个第一个用户名,并且无法向下滚动以加载所有这些用户名

我尝试了一些代码向下滚动,但它滚动主页,而我想要滚动“关注者列表”

main screen

谁能帮我向下滚动?

我试过 "send.key" 和 "browser.execute_script("arguments[0].scrollIntoView();",Element)" 但没什么用

【问题讨论】:

  • 您的问题解决了吗?
  • @vitaliis 很遗憾没有

标签: python selenium selenium-webdriver python-requests instagram


【解决方案1】:

试试这个解决方案。我现在正在使用它,所以很确定它可以工作。

# try to find the locator of the div element which contain the slider
element = browser.find_element_by_xpath('//div/parent::ul/parent::div')

# increase the distance from the top element each time one pixel
verical_ordinate = 100
for i in range(0, 50):
   print(verical_ordinate)
   browser.execute_script("arguments[0].scrollTop = arguments[1]", element, 
   verical_ordinate)
   verical_ordinate += 100
   time.sleep(1)

【讨论】:

    【解决方案2】:

    对于滚动,我通常使用这段代码,它总是有效的。

    driver.execute_script("arguments[0].scrollIntoView();",driver.find_element_by_xpath("your_xpath_selector"))
    

    首先,将 WebElement 重命名为您的自定义名称,例如重命名为 web_element,因为您的名称会造成混淆。 其次,确保这个定位器是正确的。很遗憾,我没有 Instagram 帐户,也无法验证此定位器。

    【讨论】:

    • 如果你没有instagram账号也没问题,你可以看到一个公开的帖子并控制它的html标签。我知道我的主要问题是,我找不到正确的元素来滚动它。 instgram 有很多类可以显示列表
    • 请添加您要在其中查找定位器的 html 代码。我可以帮助使用那个定位器。
    • 我怎样才能把它的照片发给你,请给我一个电子邮件或任何社交媒体帐户
    • 您可以在这里发布部分html代码作为sn-p。使用 devtools 找到您需要的部分。
    猜你喜欢
    • 2018-08-05
    • 2019-11-06
    • 2017-06-07
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 2016-11-05
    相关资源
    最近更新 更多