Изменение Bool для цикла while - PullRequest
0 голосов
/ 14 июня 2019

Я работаю над программой очистки веб-страниц, и мне нужно использовать некоторое время, и я устанавливаю ее равной Till, и она запускается несколько раз, затем, когда j становится равным определенному числу, она меняет bool на false и выходит из цикла наперейдите к следующей функции, чтобы фактически проанализировать данные.Но мне нужно, чтобы он снова вошел в цикл, когда он будет готов получить следующий URL, но если s по-прежнему равен False, он не войдет в него.Как я могу изменить s обратно на true?

class MySpider(Spider):
    # Name of Spider
    name = 'splash_spider'
    # getting all the url + ip address + useragent pairs then request them
    def start_requests(self):
        # get the file path of the csv file that contains the pairs from the settings.py
        with open(self.settings["PROXY_CSV_FILE"], mode="r") as csv_file:
           # requests is a list of dictionaries like this -> {url: str, ua: str, ip: str}
            requests = process_csv(csv_file)
            j = 1
            s = True
            for i, req in enumerate(requests):
                import pdb; pdb.set_trace()
                while s == True :
                    x = len(requests) - i
                    # Return needed url with set delay of 3 seconds
                    yield SplashRequest(url=req["url"], callback=self.parse, args={"wait": 3},
                            # Pair with user agent specified in csv file
                            headers={"User-Agent": req["ua"]},
                            # Sets splash_url to whatever the current proxy that goes with current URL  is instead of actual splash url
                            splash_url = req["ip"],
                            priority = x,
                            meta={'priority': x}  # <- check here!!
                            )

                    j = j + 1
                    if j == len(requests):
                        s = False
                        j = 1

Ответы [ 2 ]

2 голосов
/ 14 июня 2019

Не используйте логическое значение. Используйте while True:, а затем используйте break для выхода из цикла.

def start_requests(self):
    # get the file path of the csv file that contains the pairs from the settings.py
    with open(self.settings["PROXY_CSV_FILE"], mode="r") as csv_file:
       # requests is a list of dictionaries like this -> {url: str, ua: str, ip: str}
        requests = process_csv(csv_file)
        j = 1
        for i, req in enumerate(requests):
            import pdb; pdb.set_trace()
            while True :
                x = len(requests) - i
                # Return needed url with set delay of 3 seconds
                yield SplashRequest(url=req["url"], callback=self.parse, args={"wait": 3},
                        # Pair with user agent specified in csv file
                        headers={"User-Agent": req["ua"]},
                        # Sets splash_url to whatever the current proxy that goes with current URL  is instead of actual splash url
                        splash_url = req["ip"],
                        priority = x,
                        meta={'priority': x}  # <- check here!!
                        )

                j = j + 1
                if j == len(requests):
                    j = 1
                    break

Но, похоже, вам вообще не нужно while или j, используйте for _ in range(len(requests)):

Кроме того, вы должны установить x вне внутреннего цикла, поскольку он не изменяется в этом цикле.

def start_requests(self):
    # get the file path of the csv file that contains the pairs from the settings.py
    with open(self.settings["PROXY_CSV_FILE"], mode="r") as csv_file:
       # requests is a list of dictionaries like this -> {url: str, ua: str, ip: str}
        requests = process_csv(csv_file)
        for i, req in enumerate(requests):
            import pdb; pdb.set_trace()
            x = len(requests) - i
            for _ in range(len(requests)):
                # Return needed url with set delay of 3 seconds
                yield SplashRequest(url=req["url"], callback=self.parse, args={"wait": 3},
                    # Pair with user agent specified in csv file
                    headers={"User-Agent": req["ua"]},
                    # Sets splash_url to whatever the current proxy that goes with current URL  is instead of actual splash url
                    splash_url = req["ip"],
                    priority = x,
                    meta={'priority': x}  # <- check here!!
                    )
0 голосов
/ 15 июня 2019

Мне кажется, если вы просто переназначите s на false, где вы импортируете ipdb, это бы сработало:

class MySpider(Spider):
    # Name of Spider
    name = 'splash_spider'
    # getting all the url + ip address + useragent pairs then request them
    def start_requests(self):
        # get the file path of the csv file that contains the pairs from the settings.py
        with open(self.settings["PROXY_CSV_FILE"], mode="r") as csv_file:
           # requests is a list of dictionaries like this -> {url: str, ua: str, ip: str}
            requests = process_csv(csv_file)
            j = 1
            for i, req in enumerate(requests):
                s = True
                import pdb; pdb.set_trace()
                while s == True :
                    x = len(requests) - i
                    # Return needed url with set delay of 3 seconds
                    yield SplashRequest(url=req["url"], callback=self.parse, args={"wait": 3},
                            # Pair with user agent specified in csv file
                            headers={"User-Agent": req["ua"]},
                            # Sets splash_url to whatever the current proxy that goes with current URL  is instead of actual splash url
                            splash_url = req["ip"],
                            priority = x,
                            meta={'priority': x}  # <- check here!!
                            )

                    j = j + 1
                    if j == len(requests):
                        s = False
                        j = 1
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...