2

So, I made a fun, experimental Python script that puts together a gobbledegook series of characters, then looks it up on the Scrabble Dictionary using the requests module. But this is the code snippet that I'm focusing on here:

url = 'https://scrabblewordfinder.org/dictionary/'+word+'?s=t'
web = requests.get(url)
print('Found webpage')
# text = text[int(len(text)*0.75):]
if 'Not a valid word.' in web.text:
    print(''.join(word), 'is not a word')
else:
    print(''.join(word), 'is a word')
    words.append(word)
web.close()

(where word is the generated string of characters and words is the list of certified words) (Oh, and all of that code is repeated over and over again in a loop.)

This code works fine, but I'm wondering if I have to close the connection each time. I don't really understand how websites work, but I think that when you visit a different page of the same website, you never close and re-open the connection to the server; it's just that what the server displays to you changes. (In fact, this is proven by the URLs on Google Earth; the application doesn't have to reload every time you move.)

So, do I cut out the web.close part and put it after the loop? If so, will requests.get(url) open a new connection each time, or simply change the information asked of the server without closing and reopening the connection?

P.S. I tried pre-searching for potential duplicates, but the [requests] tag has hundreds of matches and I don't know enough to narrow my search. Sorry if there are any duplicates! And thank you for any help!

Palbitt
  • 121
  • 5
  • Possible dup of https://stackoverflow.com/questions/24873927/python-requests-module-and-connection-reuse. The short version: use a `Session` object instead of `requests.get` to access the site. – tdelaney Sep 16 '20 at 01:04

0 Answers0