我对此非常陌生并且一直在尝试使用此代码,但是当我使用它时,它会向我吐出这个错误
C:\Users\julia\Desktop\play>python play.py
QUESTION IS: Which of these film franchises has the most installments
OPTIONS ARE:
['Underworld', 'Resident Evil', 'Pirates of the Caribbean']
Analyzing...
Traceback (most recent call last):
File "play.py", line 117, in <module>
feed(img)
File "play.py", line 95, in feed
calculate_answer(question, answers)
File "play.py", line 75, in calculate_answer
ans_occ, = [x + y for x, y in zip(ans_occ, query_once(quest, ans))]
File "play.py", line 52, in query_once
content = get_response(query)
File "play.py", line 40, in get_response
req = urllib.request.Request(query, headers=headers)
File "C:\Python36-32\lib\urllib\request.py", line 335, in __init__
for key, value in headers.items():
AttributeError: 'tuple' object has no attribute 'items'
这是我从
获取错误的代码try:
import Image
except ImportError:
from PIL import Image, ImageGrab
import pytesseract
import time
import os
import urllib.parse
import urllib.request
class AppURLopener(urllib.request.FancyURLopener):
version = "Mozilla/5.0"
opener = AppURLopener()
response = opener.open('http://httpbin.org/user-agent')
from bs4 import BeautifulSoup
t_start = time.time()
test_folder_name = 'test_questions/'
user_agent = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
headers = ('User-Agent', 'Mozilla/5.0')
google_url = "https://www.google.com/search?q="
bbox = (23, 113, 499, 705) # specific area in the screen
def print_green(text):
print('\033[92m' + text + '\033[0m')
def make_query(text):
q = urllib.parse.quote(text)
query = ''.join([google_url, q])
return query
def get_response(query):
# print(query)
req = urllib.request.Request(query, headers=headers)
response = urllib.request.urlopen(req).read()
soup = BeautifulSoup(response, 'html.parser')
# removes all script and style elements
for script in soup(["script", "style"]):
script.decompose()
return soup.get_text().lower()
def query_once(quest, ans):
ans_occ = [0] * len(ans)
query = make_query(quest)
content = get_response(query)
for i in range(0, len(ans)):
ans_occ[i] += content.count(ans[i].lower())
return ans_occ
def query_for_all(quest, ans):
ans_occ = [0] * len(ans)
for choice in ans:
query = make_query(''.join([quest, ' %s' % choice])) # todo if 8: in choice replace with &:
content = get_response(query)
for i in range(0, len(ans)):
ans_occ[i] += content.count(ans[i].lower())
if 'missing: ' in content: # todo reduce score not continue
print('%s missing' % choice)
# continugnospio
return ans_occ
def calculate_answer(quest, ans):
print('Analyzing...')
ans_occ = [0] * len(ans)
ans_occ = [x + y for x, y in zip(ans_occ, query_once(quest, ans))]
print(ans_occ)
if max(ans_occ) == min(ans_occ) or len(ans) > 3: # todo added 'or' as temporary fix for removing extra choices from noise
ans_occ = [x + y for x, y in zip(ans_occ, query_for_all(quest, ans))]
print(ans_occ)
print('Answer is:')
if ' NOT ' not in quest: # todo never
print_green(ans[ans_occ.index(max(ans_occ))])
else:
print_green(ans[ans_occ.index(min(ans_occ))]) # if quest contains 'NOT' return min(ans_occ)
def feed(image):
text = pytesseract.image_to_string(image)
if len(text) > 0:
question, answers = text.split("?")
question = question.replace("\n", " ")
answers = [x for x in answers.split("\n") if x] # todo remove "-" answers and extra noise
print('QUESTION IS:', question)
print('OPTIONS ARE:\n', answers)
calculate_answer(question, answers)
else:
print('No question on screen')
def display_test(file):
image = Image.open(file)
feed(image)
def display_multiple_test():
test_images_folder = os.listdir(test_folder_name)
for file in test_images_folder:
if file.endswith(".png"):
display_test(''.join([test_folder_name, file]))
# tests
# display_test(''.join([test_folder_name, 'test_question_12.png']))
# display_multiple_test()
img = ImageGrab.grab(bbox)
feed(img)
# img.save('box.png')
t_end = time.time()
time_taken = t_end - t_start
print('Took: %f seconds' % time_taken)
有人可以告诉我这里需要改变什么吗?我对这种编码语言不是很有经验,所以请对我很轻松。我非常感激。
答案 0 :(得分:0)
。 Items()方法用于字典而不是元组
答案 1 :(得分:0)
使用字典设置标题而不是元组,如下所示:
headers = {'User-Agent': 'Mozilla/5.0'}
另见&#34; How do I send a custom header with urllib2 in a HTTP Request?&#34;