Scraper.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:scraper-boilerplate 作者: samatt 项目源码 文件源码
def bot_mitigation(self):
        """Three commands for bot-detection mitigation when getting a site."""
        # bot mitigation 1: move the randomly around a number of times
        window_size = self.webdriver.get_window_size()
        num_moves = 0
        num_fails = 0
        while num_moves < NUM_MOUSE_MOVES + 1 and num_fails < NUM_MOUSE_MOVES:
            try:
                if num_moves == 0:  # move to the center of the screen
                    x = int(round(window_size['height']/2))
                    y = int(round(window_size['width']/2))
                else:  # move a random amount in some direction
                    move_max = random.randint(0, 500)
                    x = random.randint(-move_max, move_max)
                    y = random.randint(-move_max, move_max)
                action = ActionChains(self.webdriver)
                action.move_by_offset(x, y)
                action.perform()
                num_moves += 1

            except MoveTargetOutOfBoundsException:
                num_fails += 1
                self.log.warning("Mouse movement out of bounds,"
                                 "trying a different offset...")

        # bot mitigation 2: scroll in random intervals down page
        scroll_down(self.webdriver)

        # mitigation 3: randomly wait so that page visits appear irregularly
        time.sleep(random.randrange(RANDOM_SLEEP_LOW, RANDOM_SLEEP_HIGH))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号