Как открыть браузер только один раз внутри для l oop? - PullRequest
0 голосов
/ 05 апреля 2020

Код:

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. Однако, поскольку он находится внутри для l oop, он продолжает открываться снова и снова. Как сделать так, чтобы он открывался только один раз, при этом остальная часть кода все еще работала ? Не нарушать весь код и не открывать URL, а открывать URL один раз и поддерживать работу кода.

Ответы [ 2 ]

0 голосов
/ 05 апреля 2020

Вы можете использовать значение 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)
0 голосов
/ 05 апреля 2020

Попробуйте:

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)
...