I have a program where the user would input their word and they'd have the definition printed before them by scraping DuckDuckGo's auto response box.
import requests
import sys
import codecs
import os
import time
def end():
steve = 1
p1 = "https://duckduckgo.com/?q="
p2 = input().replace(" ", "+")
p3 = p1 + p2
timeout = 0
def find():
global timeout
timeout += 1
print(timeout)
try:
time.sleep(1)
res = requests.get(p3)
res.raise_for_status()
playFile = open('RomeoAndJuliet1.txt', 'wb')
for chunk in res.iter_content(100000):
playFile.write(chunk)
playFile.close()
f = codecs.open('RomeoAndJuliet1.txt',encoding='utf-8')
contents = f.read()
newcontents = contents.replace('"','*').replace("'", '^')
page = newcontents
i = page.index("Abstract") + 11
defn = page[i: page.index("*", i)]
f.close
if timeout == 8:
print('timed out')
exit()
if defn == '' or defn == 't' or defn == 'rce':
find()
if defn != '' or defn != 't' or defn != 'rce' or defn != 'err':
open("demofile.txt", "w").write(defn)
print(defn)
end()
except:
print('err')
end()
find()
end()
os.remove("RomeoAndJuliet1.txt")
Occasionally I would get unwanted definitions of t, src, or just a blank character. I have adjusted that by making the program reload the page and check again.
My main issue is that when the program does have to refresh, it outputs the anticipated answer as well as all the unwanted answers listed above.
>>>
== RESTART==
bill gates
1 #Number of refresh attempts
2
3
William Henry Gates III is an American business magnate, investor, author, philanthropist, humanitarian, and principal founder of Microsoft Corporation. During his career at Microsoft, Gates held the positions of chairman, CEO and chief software architect, while also being the largest individual shareholder until May 2014.
rce
rce
>>>
I'm trying to only get the anticipated answer and store it in a text file, but it keeps getting overwritten by the unwanted answer
I changed
if defn != '' or defn != 't' or defn != 'rce' or defn != 'err':
open("demofile.txt", "w").write(defn)
print(defn)
end()
to
if defn != '' and defn != 't' and defn != 'rce' and defn != 'err':
open("demofile.txt", "w").write(defn)
print(defn)
end()
I only changed the or's to and's.
Now demofile.txt contains the anticipated answer and this if part will only be executed if defn is not equal to the specific values
The file was overwritten for two reasons in conjunction.
The first reason being you used or in combination with !=, so when one !=-clause was satisfied which would always be the case since defn cannot be all of them at the same time it would execute this if part.
The second reason being your call to find() inside find() here:
if defn == '' or defn == 't' or defn == 'rce':
find()
Since that appears before:
if defn != '' and defn != 't' and defn != 'rce' and defn != 'err':
open("demofile.txt", "w").write(defn)
print(defn)
end()
After the first call to find() failed to return the anticipated answer it would then call find(), which would (maybe) call find() again, and so on, until maybe the anticipated answer would be returned by one of those find() calls and thereby ending the consecutive find() calls. The "failed" find() call(s) would then overwrite the "correct" find() call answer because after the "correct" find() call is finished, the other find() calls get executed in reversed order of which they were initially called until the respective ends of the respective functions are reached .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With