首页 > 解决方案 > selenium 从 instagram 设置中抓取请求列表的问题

问题描述

我从 selenium 的 instagram 设置中抓取请求列表时遇到问题。这是我的代码:

from time import sleep
from selenium import webdriver

#user_info
user = 'xxx'
password = 'xxx'

#login
bot = webdriver.Firefox()
bot.get('https://instagram.com')
sleep(2)
username_input = bot.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input')
sleep(1)
username_input.send_keys(user)
sleep(1)
password_input = bot.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input')
sleep(1)
password_input.send_keys(password)
sleep(1)

bot.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button/div').click( ) 睡觉(3)

#try_to_scraping_requested_profile
bot.get('https://instagram.com/accounts/access_tool/current_follow_requests')
sleep(2)
viewmore = 0
while viewmore <4:
    bot.find_element_by_xpath('/html/body/div[1]/div/div/section/main/div/article/main/button').click()
    viewmore += 1
    sleep(1)




#this_is_where_the_problem_happend
requestlist =[]
users = bot.find_elements_by_class_name('-utLF')
print(users.text) #this_line_will_return_nothing

for user in users :
    
    requestlist.append(user.text)
#also_the_requestlist_will_be_empty
print(requestlist)

标签: pythonseleniumselenium-webdriverbots

解决方案


既然你提到

print(users.text) #this_line_will_return_nothing

users在你的情况下是listin Python selenium。你不能.text就一个list

为此:

for user in users :
   requestlist.append(user.text)
#also_the_requestlist_will_be_empty
print(requestlist)

我建议先打印列表的大小:-

print(len(users)) 

看看里面有没有东西。我认为它没有任何东西,所以尝试不同的定位器。


推荐阅读