【问题标题】:Python operator filter dosen't work (Selenium)Python 运算符过滤器不起作用(Selenium)
【发布时间】:2021-04-22 00:53:52
【问题描述】:

大家好,我正在尝试使用 selenium 和 python 过滤一些 instagram 用户,目标是关注所有用户的关注者多于关注者,但我不知道他为什么返回相同的结果:

以该帐户为例:

通常他必须打印我“追随者多于追随者”

我的代码:

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

我的控制台日志返回:

感谢您的帮助:)

【问题讨论】:

    标签: python selenium if-statement instagram


    【解决方案1】:

    您的第二个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
    

    【讨论】:

    • 啊,我傻了,它现在为我工作,非常感谢:)
    • 如果它有效,请接受/支持答案。这就是 SO 的工作原理。
    【解决方案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来控制循环。但是对于上面的代码,因为这些是循环中的最后一个语句,而且还有条件,你现在不需要这些控制语句。

    【讨论】:

    • 是的,也在工作,非常感谢兄弟;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多