Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 硒罐';我无法在Instagram上获取所有元素_Python_Selenium_Xpath_Selenium Chromedriver - Fatal编程技术网

Python 硒罐';我无法在Instagram上获取所有元素

Python 硒罐';我无法在Instagram上获取所有元素,python,selenium,xpath,selenium-chromedriver,Python,Selenium,Xpath,Selenium Chromedriver,我正在使用Python selenium创建一个脚本,用于抓取instagram用户帖子。如果用户有62篇文章,我想得到所有的62篇文章 我尝试向下滚动,直到加载所有post,并使用xpath及其作品获取元素/post。但只有29个要素/岗位,而不是全部62个要素/岗位 driver.get("https://instagram.com/celmirashop/") #scroll until all post loaded scroll() wait = We

我正在使用Python selenium创建一个脚本,用于抓取instagram用户帖子。如果用户有62篇文章,我想得到所有的62篇文章

我尝试向下滚动,直到加载所有post,并使用xpath及其作品获取元素/post。但只有29个要素/岗位,而不是全部62个要素/岗位

    driver.get("https://instagram.com/celmirashop/")

    #scroll until all post loaded
    scroll()
    wait = WebDriverWait(driver, 15)
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.eLAPa")))

    time.sleep(30)

    #getting list cards of posts
    list_cards = driver.find_elements_by_xpath("//*[@class='v1Nh3 kIKUG  _bz0w']")
    print(len(list_cards))

如果用户有62篇文章,我想得到62篇(全部)文章的元素。他们设计应用程序的方式很难刮。这些元素是延迟加载的,因此当您滚动时,某些元素也可能消失

我会说使用xpath泛型和不变的,比如
//a//img
,因为它们会再次将类名更改为随机的


另外,由于您已经有了滚动的方法,请从头开始。记录所有元素并滚动更多元素,再次记录并刮取更多元素。在滚动instagram时,循环直到找到页面元素的结尾,如
//footer

将显示新的12个图像,但instagram将删除12个传递的图像。我找到了解决方案,滚动时保存12个图像(每次向下滚动)。因此,在instagram删除已通过的12幅图像之前,我已将这些图像保存在variabel上

driver.get("https://instagram.com/celmirashop/")


semua_url_lengkap = []
semua_url_post = []
nomor=1
for i in range(50):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    print(nomor)
    nomor+=1
    #mendapatkan list tiap cards update status
    article = driver.find_element_by_tag_name("article")
    list_cards = article.find_elements_by_tag_name("a")

    for item in list_cards:

        url_lengkap=item.get_attribute("href")
        semua_url_lengkap.append(url_lengkap)

        segmen = url_lengkap.rsplit('/', 2)
        semua_url_post.append(segmen[1])


print(len(semua_url_post))
print(semua_url_post)