简体   繁体   中英

Finding post+comment block on instagram (selenium, python, chromedriver)

My goal is to be able to find the sizes & location on page of all of the Instagram (post+comment) blocks of users I follow. (large rectangle block of page below with picture on left and comments on right)

An example of this is the following:

  1. go to https://www.instagram.com/zuck/

  2. click on the first post

在此处输入图片说明

  1. I am trying to driver.find_element the image with comments block in the center of the page above so that I can find the size & location on page of the block. The xpath I see is ( //*[@id="react-root"]/section/main/div/div/article/div[1] ) and the div class begins with _97aPb , however, when I refresh or go to a different user these change.

So, I'm looking for an alternative way to find that element. I want to be able to click to the next post and be able to find the (post +comment) block for all posts.

How to get the article:

Depending if how opened the post, the contents will be under the div with @role="dialog" or @id="react-root" . If you open the post directly, there is no dialog but if you search for '//div[@role="dialog" or @id="react-root"]//article then the last one will be the post you are looking for.

So:

article = driver.find_elements_by_xpath('//div[@role="dialog" or @id="react-root"]//article')[-1]

How to get the next image in the article:

next_image_arrow = article.find_element_by_xpath('.//*[contains(@class, "coreSpriteRightChevron")]')

How to select the next article (available only when article opened in modal):

next_article_button = driver.find_element_by_xpath('//a[contains(@class, "coreSpriteRightPaginationArrow")]')

How to get the comments:

all_comments = article.find_elements_by_xpath('./div/section[2]/following-sibling::div/ul/ul')

The Xpath above means this:

  • /div/section[2] << in the article node, get all nodes which have a direct div child node which have at least 2 direct section tagged child nodes and select the second one
  • /following-sibling::div/ul/ul << take the found section nodes and if the section node is followed by a div and it has direct ul child nodes which have direct ul child nodes, return them

I hope this answers the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM