使用Python和Selenium抓取喜欢Instagram帖子的帐户名称,但只有11个名称返回



我正在尝试使用Selenium和Python抓取喜欢Instagram帖子的帐户名称。

https://www.instagram.com/p/B57dJp3gIGw/

没有返回任何错误,我成功抓取,但只有喜欢该帖子的前 11 名返回,而喜欢该帖子的人有 40 人。我想知道原因是什么,我该如何解决?

liker_list = []
likers = driver.find_elements_by_class_name("qyrsm")
for n in likers:
#scrape the name of the likers
liker = n.find_element_by_class_name("_4EzTm").get_attribute("textContent")
liker_list.append(liker)

print(liker_list)

这是结果(liker_list(

['rycmtn', 'asat0oo', 'misswanderwolf', 'renkuga0202', 'na_na972', 'natsu_5550', 'hachibayinternational_inc', 'mi.kyoung.jeon', 'crane42195', 'michi___kusa', 'ankhcarpediem']

很可能页面需要向下滚动才能查看所有喜欢并抓取它们。

要无限滚动的块如下,您可以将现有内容添加到此循环的内部,以便在滚动时抓取数据。

def scroll():
SCROLL_PAUSE_TIME = 1
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
scroll()

您将在 time.sleep(SCROLL_PAUSE_TIME( 行之后添加代码。

希望这有帮助。

相关内容

最新更新