【问题标题】:How do I open a browser only once inside a for loop?如何在 for 循环中只打开一次浏览器?
【发布时间】:2023-04-03 23:00:01
【问题描述】:

代码是:

import requests
from time import sleep
import webbrowser
from termcolor import colored


print(colored('Lowest Priced Limited\n---------------------\n', 'green'))

while True:
   lowestprice = 1234567890
   for limited in requests.get('https://search.roblox.com/catalog/json?Category=2&Subcategory=2&SortType=4&Direction=2').json():
       price = int(limited['BestPrice'])
       if price < lowestprice:
           limitedname = limited['Name']
           limitedurl = limited['AbsoluteUrl']
           lowestprice = price
   print(colored(f"{limitedname}: {lowestprice}\n{limitedurl}\n"))
   sleep(1)

   if lowestprice <= 300:
      webbrowser.open(limitedurl, new=2)

如您所见,最后一个 if 语句打开了 URL。但是,由于它位于 for 循环中,因此它会一遍又一遍地重新打开。如何让它只打开一次,同时保持其余代码仍在运行?不是破坏整个代码并打开 URL,而是打开 URL 一次,并保持代码运行。

【问题讨论】:

    标签: python loops for-loop if-statement


    【解决方案1】:

    试试这个:

    import requests
    from time import sleep
    import webbrowser
    from termcolor import colored
    
    
    print(colored('Lowest Priced Limited\n---------------------\n', 'green'))
    count=0
    while True:
       lowestprice = 1234567890
       for limited in requests.get('https://search.roblox.com/catalog/json?Category=2&Subcategory=2&SortType=4&Direction=2').json():
           price = int(limited['BestPrice'])
           if price < lowestprice:
               limitedname = limited['Name']
               limitedurl = limited['AbsoluteUrl']
               lowestprice = price
       print(colored(f"{limitedname}: {lowestprice}\n{limitedurl}\n"))
       sleep(1)
    
       if (lowestprice <= 300 and count==0):
          webbrowser.open(limitedurl, new=2)
          count+=1
    

    要以最低价格打开网络浏览器,请尝试:

    import requests
    from time import sleep
    import webbrowser
    from termcolor import colored
    
    
    print(colored('Lowest Priced Limited\n---------------------\n', 'green'))
    count=0
    lpep=0
    lpepurl=''
    while True:
       lowestprice = 1234567890
       for limited in requests.get('https://search.roblox.com/catalog/json?Category=2&Subcategory=2&SortType=4&Direction=2').json():
           price = int(limited['BestPrice'])
           if price < lowestprice:
               limitedname = limited['Name']
               limitedurl = limited['AbsoluteUrl']
               lowestprice = price
       print(colored(f"{limitedname}: {lowestprice}\n{limitedurl}\n"))
       sleep(1)
       if lowestprice <= 300 and count == 0:
           lpep = lowestprice
           lpepurl = limitedurl
           webbrowser.open(lpepurl, new=2)
           count+=1
       elif lowestprice <= 300 and lowestprice < lpep:
           lpep = lowestprice
           lpepurl = limitedurl
           webbrowser.open(lpepurl, new=2)
    

    【讨论】:

    • 这很好用。但是,假设最低价格是 250。然后 URL 打开一次,代码继续运行。但是,如果最低价格再次发生变化,则 URL 不会再次打开。是否有可能在价格变化时重新开放?
    • 您希望以尽可能低的价格打开网址?
    • 现在,只要最低价低于 300,URL 就会打开,对吧?而且,代码还在继续。随着代码的进行,假设最低价格变为 290 或 260 或 200 等等。该 URL 不再为我打开。每次价格变化低于 300 时,我都会尝试打开 URL,这样我就可以比其他人更便宜地购买。
    • 立即尝试。我已经添加了
    • 就你现在说的,不可能只开一次,因为我们没有说什么时候降价。代码现在以低于之前最低价格的价格打开
    【解决方案2】:

    您可以使用bool 值来确定它是否已打开一次:

    import requests
    from time import sleep
    import webbrowser
    from termcolor import colored
    
    
    print(colored('Lowest Priced Limited\n---------------------\n', 'green'))
    
    openedBrowser = False
    
    while True:
        ... #other code
    
        if lowestprice <= 300 and not openedBrowser:
            openedBrowser = True
            webbrowser.open(limitedurl, new=2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 2022-01-23
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      相关资源
      最近更新 更多