【发布时间】:2020-08-14 14:45:00
【问题描述】:
我尝试使用 find_elements_by_xpath 从网站中提取 10 个元素,但它总是提取前四个元素。
这是我要提取的内容的屏幕截图(标有红点的行,它停在标有红十字的行):
这是我的python代码:
################################################################################
# Déclaration et initialisation des variables globales
################################################################################
currentDirectory = os.getcwd()
path_to_website = "URL" #changed for stackoverflow question
path_to_chrome_profile = "path_to_chrome_profile" #changed for stackoverflow question
xpath_suggestions_text = "//li[@class='search-result search-result__occluded-item ember-view']"
################################################################################
# Navigation sur google Chrome
################################################################################
# lancement de chrome avec le profil par défaut
options_ch = webdriver.ChromeOptions()
options_ch.add_argument("user-data-dir="+path_to_chrome_profile)
browser_chrome = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options_ch)
# on va sur la page réseau de linkedIn
browser_chrome.get(path_to_website )
list_elements = browser_chrome.find_elements_by_xpath(xpath_suggestions_text)
print(len(list_elements )) #prints 4 instead of 10
for profils in list_elements :
print(element.text)
print("////////")
browser_chrome.quit()
我尝试了很多东西,但没有任何效果..有什么线索吗? (我是网络抓取的初学者,请放纵:x)
谢谢大家
AJT
【问题讨论】:
-
你试过只按类名搜索吗
-
是的,我试过了,我没有找到任何东西 x),函数返回一个空列表
-
您确定所有 10 个元素最初都存在于列表中吗?我猜其他元素是动态添加的
-
不知道是不是这样,也许你是对的。我如何检查然后提取这些动态元素?
-
不知道该告诉你什么。也许最好发布实际的 html。
标签: python selenium web-scraping