Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Word Search Solver
#1
Hello all,

First time here. So I've pieced together some code and can't quite figure out how to get it to work. It's not throwing any errors, but it isn't printing any results either. Basically the code reaches out, logs into a website, grabs a wordsearch grid, and is supposed to perform a word search for words 6 characters or longer. Any pointers?

#begin module imports
import re, os, sys, time, getpass
try:
  import requests
except:
  exit('[-] Importing Requests module failed')


#Begin with login to WordSearchWeb
class WordSearchWeb:

  loginUrl  = 'http://www.WordSearchWeb.net/login'
  challUrl  = 'http://www.WordSearchWeb.net/index.php'
  gridsUrl = 'http://www.WordSearchWeb.net/lettergrid/generate.php' 

  def __init__(self, username, password, s) : 
    self.login(username, password, s)
    self.sidebar(s)
    self.gridSRC = self.getgrid(s)
    self.searchgrid(self.gridSRC)
    print ' '.join(sorted(set(word for (word, path) in self.solve())))
    
  def login(self, username, password, s):
    r = s.get(self.loginUrl)
    r = s.post(self.loginUrl, data = {'username' : username, 'password' : password, 'login' : 'Login'})
    if 'Welcome back to WordSearchWeb' in r.text:
      print('[+] Login Successful')
      c = r.request.headers['Cookie']
      self.Cookie = c
      return s

  def sidebar(self, s):
    print('[+] Closing Side bar for faster page load')
    r = s.get('http://www.WordSearchWeb.net/index.php?mo=WordSearchWeb&me=Sidebar2&rightpanel=0')
  
  def getgrid(self, s):
    print('[+] Getting source code of generate.php')
    r = s.get(self.gridsUrl)
    gridSRC = r.content
    gridSRC = gridSRC.replace('<pre>','')
    gridSRC = gridSRC.replace('</pre>','')
    print gridSRC
    gridSRC = gridSRC.replace('\n',' ')
    gridSRC = str(gridSRC[1:len(gridSRC)-1])
    return gridSRC

  def searchgrid(self, grid):
    gridSRC=self.getgrid(s)
    self.grid = grid
    grid = gridSRC.split()
    print grid
    nrows, ncols = len(grid), len(grid[0])
    alphabet = ''.join(set(''.join(grid)))
    bogglable = re.compile('[' + alphabet + ']{6,}$', re.I).match

    self.words = set(word.rstrip('\n') for word in open('words') if bogglable(word))
    prefixes = set(word[:i] for word in self.words
                   for i in range(2, len(word)+1))

  def solve(self):
    grid = self.grid
    for y, row in enumerate(grid):
      for x, letter in enumerate(row):
        for result in self.extending(letter, ((x, y),)):
          yield result

  def extending(self, prefix, path):
    if prefix in self.words:
      yield (prefix, path)
      for (nx, ny) in neighbors(path[-1]):
        if (nx, ny) not in path:
          prefix1 = prefix + grid[ny][nx]
          if prefix1 in prefixes:
            for result in extending(prefix1, path + ((nx, ny),)):
              yield result

  def neighbors((x, y)):
    for nx in range(max(0, x-1), min(x+2, ncols)):
      for ny in range(max(0, y-1), min(y+2, nrows)):
        yield (nx, ny)


u = 'myuser'
p = 'mypass'
s = requests.Session()

WordSearchWeb(u,p,s)
Huh Huh Huh
Reply
#2
I'm not big on web scripts, but if it's not printing anything, then it's not printing line 27. That would indicate a login error. If it is printing line 27, please be more clear about exactly what happens when you run the script. Also, I would assign the instance of WordSearchWeb you create to a variable name. The you can examine the attributes to make sure things are happening correctly at each step of the process.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Inside your login function, print out the response you get.  That's the first time you try printing anything to console, so logging in is probably failing.
Reply
#4
(Oct-09-2017, 09:42 PM)nilamo Wrote: Inside your login function, print out the response you get.  That's the first time you try printing anything to console, so logging in is probably failing.

I should probably have been a little more clear in my post. This is what prints:

[Image: c27xeEW.png]

It get to the point where it gets the page contents and places each line into a list as an item, but the script is failing to solve the puzzle. I'm not sure that I'm passing the data between the functions properly.
Reply
#5
Do you have a sample grid we can try it out with?  As is, it's not runnable (since we don't have a user/pass), which means we can only eyeball it without being able to run it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,126 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Sudoku Solver, please help to solve a problem. AdithyaR 5 2,102 Oct-28-2021, 03:15 PM
Last Post: deanhystad
  building a sudoku solver usercat123 7 2,758 Oct-01-2021, 08:57 PM
Last Post: deanhystad
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,488 Aug-12-2021, 04:25 PM
Last Post: palladium
  search for more than one word using lambda illmattic 2 2,013 Nov-13-2020, 11:44 AM
Last Post: illmattic
  unable to use result of solver in another function ross1993hall 0 1,413 Aug-10-2020, 10:29 AM
Last Post: ross1993hall
  Complex word search multiple files Kristenl2784 0 1,582 Jul-18-2020, 01:22 PM
Last Post: Kristenl2784
  Partial Word Search Kristenl2784 2 2,119 Jun-29-2020, 08:26 PM
Last Post: Kristenl2784
  Python Speech recognition, word by word AceScottie 6 15,989 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Trouble with Sudoku Solver Techmokid 2 2,144 Apr-08-2020, 07:55 AM
Last Post: Techmokid

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020