多索引答案Python 3
我有一个程序,用户可以在其中输入他们的单词,然后通过刮掉DuckGo的自动响应框,将定义打印在他们面前多索引答案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
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部分
由于两个原因,该文件被覆盖
- 第一个原因是您将
或
与
结合使用=代码>,所以当一个<代码>=代码>-子句得到满足,这将始终是这样的,因为
不能同时是所有的,它将执行此if部分defn
- 第二个原因是在find()中调用find():
既然之前出现过:
在第一次调用if defn != '' and defn != 't' and defn != 'rce' and defn != 'err': open("demofile.txt", "w").write(defn) print(defn) end()
find()。“失败的”
调用按与最初调用相反的顺序执行,直到到达相应函数的相应末端find()
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()