程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 selenium python 收集 Insagram 用户名大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 SELEnium python 收集 Insagram 用户名?

开发过程中遇到使用 SELEnium python 收集 Insagram 用户名的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 SELEnium python 收集 Insagram 用户名的解决方法建议,希望对你解决使用 SELEnium python 收集 Insagram 用户名有所启发或帮助;

我正在尝试保存在 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('@R_419_5979@') for elem in elems]

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

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

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

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

@H_750_2@main screen

谁能帮我向下滚动?

我尝试了 "send.key" 和 "browser.execute_script("arguments[0].scrollintoVIEw();",Element)" 但没有任何用处

解决方法

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

# 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)
,

对于滚动,我通常使用此代码并且它始终有效。

driver.execute_script("arguments[0].scrollIntoView();",driver.find_elemenT_By_xpath("your_xpath_SELEctor"))

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

大佬总结

以上是大佬教程为你收集整理的使用 selenium python 收集 Insagram 用户名全部内容,希望文章能够帮你解决使用 selenium python 收集 Insagram 用户名所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。