Python运算符筛选器不工作(Selenium)

2024-05-10 10:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,伙计们,我正在尝试用selenium&;过滤一些instagram用户;python的目标是跟踪所有用户,但我不知道为什么他会返回相同的结果:

例如,此帐户:

enter image description here

通常情况下,他必须告诉我“有更多的追随者而不是追随者”

我的代码:

followers = []    #list of url users
for follower in followers:
    #Iterate into the list
    scrap_follower = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[2]/a/span').get_attribute("title")
    
    scrap_following = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[3]/a/span').text
    
    
    follower_count = int(scrap_follower)
    following_count = int(scrap_following)
    
    browser.get(follower)
    sleep(2)
    
    followButton = browser.find_element_by_css_selector('button')
    
    print(follower_count)
    print(following_count)     
    
    if follower_count == 0:
        pass
        print("0 followers")
        
        if follower_count > following_count :
            print("have more followers than following")
            pass
    else:
        print("eligible")
            #Go to follow

我的控制台日志返回:

enter image description here

谢谢你的帮助:)


Tags: 用户divbrowserbycountsectionelementfind
2条回答

我不确定是否粘贴了错误的缩进,但代码中的逻辑似乎有点错误

if follower_count == 0:
    pass
    print("0 followers")
        
    if follower_count > following_count :
        print("have more followers than following")
        pass
else:
    print("eligible")
    #Go to follow

将此更改为以下内容可能会有所帮助:

if follower_count == 0:
    print("0 followers")
else:
    if follower_count > following_count :
        print("have more followers than following")
    else:
        print("eligible")

pass语句只是一个空语句,您可以使用continuebreak来控制循环。但对于上面的代码,这些是循环中的最后一条语句,也带有条件语句,到目前为止,您不需要这些控制语句

第二个if语句需要与第一个语句对齐,否则它将永远不会执行,除非满足if follower_count == 0:条件

您的代码:

follower_count = 1500
following_count = 1000

print(follower_count)
print(following_count)     

if follower_count == 0:
    pass
    print("0 followers")
    
    if follower_count > following_count :
        print("have more followers than following")
        pass
else:
    print("eligible")

结果(不正确):

1500
1000
eligible

正确的代码(第二个if语句与第一个if语句对齐)

follower_count = 1500
following_count = 1000

print(follower_count)
print(following_count)     

if follower_count == 0:
    pass
    print("0 followers")
    
if follower_count > following_count :
    print("have more followers than following")
    pass
else:
    print("eligible")

结果:

1500
1000
have more followers than following

相关问题 更多 >