Use a headless browser Chrome Gets puzzle team club riddle puzzle game

Zero, what tools crawl websites

  The previous two games riddle are seeing, manually entered, this puzzle to bring some inconvenience. Especially the kind of special daily battle and the like, are great riddle, write a very time-consuming. Is there any way to quickly get the riddle, and the riddle direct output to a file? The answer is reptiles, web crawl.

  Just puzzle team web crawlers measures Anti club's done a great job, there is no information on the web page of the riddle, captured packet analysis could not (I would say it is too much the number of packets), can only view with headless device.

  Started phantomJS, get Python code page code section as follows:

def getChessByPhantomJS():
    driver = webdriver.PhantomJS()
    driver.get('https://www.puzzle-dominosa.com/?size=8')
    source = driver.page_source
    driver.quit()
#
View Code

  However, operating results unhappy, and ultimately only to the basic template for a web page is not the riddle.

  With Chrome effect on how it? (I do not know how to configure the chrome headless browser can turn right baidu)

DEF getChessByChrome (): 
    path = R & lt ' D: \ chromedriver.exe ' 
    chrome_options = the Options ()
     # latter two are fixed wording must be written so 
    chrome_options.add_argument ( ' --headless ' ) 
    chrome_options.add_argument ( ' --disable - GPU ' ) 
    Driver = webdriver.Chrome (= executable_path path, chrome_options = chrome_options)
     the try : 
        driver.get ( ' https://www.puzzle-dominosa.com/?size=8 ' )
     the except Exception AS E:
         Print(e)
    source = driver.page_source
    driver.quit()
    return source
View Code

  Operating results (as it is to run the process, because this has not quit B)

DevTools listening on ws://127.0.0.1:62344/devtools/browser/8c9f8f4a-407a-4045-b
41c-b9f898d4d37b
[1203/174652.884:INFO:CONSOLE(1)] "Uncaught TypeError: window.googletag.pubads i
s not a function", source: https://www.puzzle-dominosa.com/build/js/public/new/d
ominosa-95ac3646ef.js (1)
View Code

  You can add a timeout to exit the program:

DEF getChessByChrome (): 
    path = R & lt ' D: \ chromedriver.exe ' 
    chrome_options = the Options ()
     # latter two are fixed wording must be written so 
    chrome_options.add_argument ( ' --headless ' ) 
    chrome_options.add_argument ( ' --disable - GPU ' ) 
    Driver = webdriver.Chrome (= executable_path path, chrome_options = chrome_options)
     the try : 
        driver.set_page_load_timeout ( 30 ) 
        driver.get ( ' https://www.puzzle-dominosa.com/?size=8 ' )
    except Exception as e:
        print(e)
    source = driver.page_source
    driver.quit()
    return source
View Code

  This allows the code to the page analysis function, the output of the riddle.

First, how to get dominosa riddle

  But it did not end here, what we want is the riddle. To this end, we need to analyze the code:

 

 

 Figure 1.dominosa game of riddle Code

  See it? Riddle here directly reflected in the name of the class code, corresponding to the cell 3 of the riddle 3, and when the sibling elements per unit length than the riddle, the riddle will wrap.

  Code can be written:

def solve():
    source = getChessByChrome()
    htree = etree.HTML(source)
    chessSize = len(htree.xpath('//div[@id="game"]/div/div/div/..'))
    puzzleId = htree.xpath('//div[@class="puzzleInfo"]/p/span/text()')[0]
    x = (round((4 * chessSize + 1)**0.5) - 1) // 2
    print(x)
    print(x+1)
    chess = ''
    for i,className in enumerate(htree.xpath('//div[@id="game"] / Div / div / div / ..')):
        value = className.xpath('./@class')[0].split(' ')[1][4:]
        if i % (x+1) == x:
            chess += value + '\n'
        else:
            chess += value + ' '
    with open('dominosaChess' + puzzleId + '.txt','w') as f:f.write(chess[:-1])
View Code

  So you can get use Dancing link X (chain dance) to solve dominosa game riddle inside the requirements of this document.

  Incidentally, here, in order to facilitate inquiries riddle, the output file name with a riddle ID.

Second, how to get the star battle riddle

  Get in line to use a depth-first search DFS solving star battle game riddle inside this file required to spend doing something.

  Let's see the figure below it:

 

 FIG 2.star battle riddle Code

  Here's the riddle of code class name has a certain significance, such as the left represents the dividing line bl, br represents the right side of the dividing line.

  Here only provides us with a dividing line, what we need is the kind of labeling each box belongs to which block arrangement. To do this, we need to use the BFS, breadth-first search.

def solve():
    if url.find('size=') == -1:
        limit = 1
    else:
        size = url.split('size=')[1]
        size = int(size)
        if size >= 1 and size <= 4:
            limit = 1
        elif size <= 6:
            limit = 2
        elif size <= 8:
            limit = 3
        else:
            limit = size - 5
    source = getChessByFile()
    htree = etree.HTML(source)
    chessSize = len(htree.xpath('//div[@id="game"]/div/div'))
    puzzleId = htree.xpath('//div[@class="puzzleInfo"]/p/span/text()')[0]
    chessSize = round(chessSize**0.5)
    chess = [[-1 for _ in range(chessSize)] for __ in range(chessSize)]
    borderss = [['' for _ in range(chessSize)] for __ in range(chessSize)]
    chessStr = ''
    maxBlockNumber = 0
    # br: on the right; bl: on the left; bb: on the down; bt: on the up
    for i,className in enumerate(htree.xpath('//div[@id="game"]/div/div[contains(@class,"cell")]')):
        x = i // chessSize
        y = i % chessSize
        value = className.xpath('./@class')[0]
        if value[:4] != 'cell':
            continue
        value = value.replace('cell selectable','')
        value = value.replace('cell-off','')
        borderss[x][y] = value
    for i in range(chessSize):
        for j in range(chessSize):
            if chess[i][j] != -1:
                continue
            queue = [(i, j)]
            chess[i][j] = str(maxBlockNumber)
            while len(queue) > 0:
                oldQueue = deepcopy(queue)
                queue = []
                for pos in oldQueue:
                    x, y = pos[0], pos[1]
                    #
                    if x > 0 and borderss[x][y].find('bt') == -1 and chess[x-1][y] == -1:
                        queue.append((x-1, y))
                        chess[x-1][y] = chess[i][j]
                    #
                    if x < chessSize - 1 and borderss[x][y].find('bb') == -1 and chess[x+1][y] == -1:
                        queue.append((x+1, y))
                        chess[x+1][y] = chess[i][j]
                    #
                    if y > 0 and borderss[x][y].find('bl') == -1 and chess[x][y-1] == -1:
                        queue.append((x, y-1))
                        chess[x][y-1] = chess[i][j]
                    #
                    if y < chessSize - 1 and borderss[x][y].find('br') == -1 and chess[x][y+1] == -1:
                        queue.append((x, y+1))
                        chess[x][y+1] = chess[i][j]
                    #
            maxBlockNumber += 1
    chessStr = '\n'.join(' '.join(chessRow) for chessRow in chess)
    with open('starBattleChess' + puzzleId + '.txt','w') as f:f.write(str(limit)+'\n'+chessStr)
View Code

  这里为了以后使用谜面方便,使用了稍微不兼容上述格式的排布:每行的星星数+排布

Guess you like

Origin www.cnblogs.com/dgutfly/p/11978537.html