Python 运算符过滤器不起作用 (Selenium)
Python operator filter dosen't work (Selenium)
大家好,我正在尝试使用 selenium 和 python 过滤一些 Instagram 用户,目标是关注所有用户的关注者多于关注者,但我不知道为什么他 return我同样的结果:
例如这个帐户:
通常他必须打印我“关注者多于关注者”
我的代码:
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
我的控制台日志return:
感谢您的帮助:)
您的第二个 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
我不确定你是否粘贴了错误的缩进,但你代码中的逻辑似乎没有错。
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
语句只是空语句,可以用continue
或break
来控制循环。但是对于上面的代码,因为这些是循环中的最后语句并且还带有条件,您现在不需要这些控制语句。
大家好,我正在尝试使用 selenium 和 python 过滤一些 Instagram 用户,目标是关注所有用户的关注者多于关注者,但我不知道为什么他 return我同样的结果:
例如这个帐户:
通常他必须打印我“关注者多于关注者”
我的代码:
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
我的控制台日志return:
感谢您的帮助:)
您的第二个 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
我不确定你是否粘贴了错误的缩进,但你代码中的逻辑似乎没有错。
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
语句只是空语句,可以用continue
或break
来控制循环。但是对于上面的代码,因为这些是循环中的最后语句并且还带有条件,您现在不需要这些控制语句。