多索引答案Python 3

多索引答案Python 3,python,python-3.x,Python,Python 3.x,我有一个程序,用户可以在其中输入他们的单词,然后通过刮掉DuckGo的自动响应框,将定义打印在他们面前 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 ti

我有一个程序,用户可以在其中输入他们的单词,然后通过刮掉DuckGo的自动响应框,将定义打印在他们面前

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")
偶尔我会得到
t
src
的不需要的定义,或者只是一个空白字符。我通过让程序重新加载页面并再次检查来调整它

我的主要问题是,当程序确实需要刷新时,它会输出预期的答案以及上面列出的所有不需要的答案

>>> 
== 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
>>> 
我试图只获取预期的答案并将其存储在文本文件中,但它不断被我更改的不需要的答案覆盖

if defn != '' or defn != 't' or defn != 'rce' or defn != 'err':
        open("demofile.txt", "w").write(defn)
        print(defn)
        end()

我只将
更改为

现在
demofile.txt
包含预期的答案,并且只有
defn
不等于特定值时才会执行此if部分

由于两个原因,该文件被覆盖

  • 第一个原因是您将
    结合使用=,所以当一个<代码>=-子句得到满足,这将始终是这样的,因为
    defn
    不能同时是所有的,它将执行此if部分

  • 第二个原因是在find()中调用find():

    既然之前出现过:

    if defn != '' and defn != 't' and defn != 'rce' and defn != 'err':
            open("demofile.txt", "w").write(defn)
            print(defn)
            end()
    
    在第一次调用
    find()。“失败的”
    find()
    调用按与最初调用相反的顺序执行,直到到达相应函数的相应末端


请不要破坏您的帖子,为他人做更多的工作。通过在Stack Exchange(SE)网络上发布,您已经在a下授予SE分发内容的不可撤销权利(即,无论您未来的选择如何)。根据SE政策,分发非故意破坏版本。因此,任何故意破坏行为都将恢复原状。请参阅:。如果允许删除,则在帖子下方左侧有一个“删除”按钮,但它仅在浏览器中,而不是移动应用程序中。
if defn == '' or defn == 't' or defn == 'rce':
        find()
if defn != '' and defn != 't' and defn != 'rce' and defn != 'err':
        open("demofile.txt", "w").write(defn)
        print(defn)
        end()