Last active
January 28, 2021 14:02
-
-
Save Andiology/553e7bd632aa726c06834199a139b30c to your computer and use it in GitHub Desktop.
Instagram number of likes and comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 首先,以下是我們本次需要的套件,先import進來。 | |
from selenium import webdriver | |
from selenium.webdriver.common.action_chains import ActionChains | |
# browser = webdriver.Chrome() | |
url = 'https://www.instagram.com/bbcnews/' | |
browser.get(url) # 前往該網址 | |
post_url = '/p/CEriQnOMwW9/' | |
find = False | |
# 不在目前的網頁元素裡,則往下滑,加載新貼文 | |
while not find: | |
try: | |
# 找到對應的貼文,鼠標移入 | |
post_elem = browser.find_element_by_xpath('//a[@href="'+str(post_url)+'"]') | |
action = ActionChains(browser) | |
action.move_to_element(post_elem).perform() | |
# 找到需要的網頁元素 | |
n_like_elem = browser.find_elements_by_class_name('-V_eO') | |
# 取得讚數、留言數 | |
n_like = n_like_elem[0].text | |
n_comment = n_like_elem[1].text | |
# 找到之後就可以回傳‘找到了’ | |
find = True | |
except: | |
# 找不到就往下滑 | |
scroll = 'window.scrollBy(0,250)' | |
browser.execute_script(scroll) | |
continue | |
print('讚數:', n_like) | |
print('留言數:', n_comment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment