Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议:输出格式化 #7

Open
heishanmao opened this issue Jan 10, 2018 · 3 comments
Open

建议:输出格式化 #7

heishanmao opened this issue Jan 10, 2018 · 3 comments

Comments

@heishanmao
Copy link

heishanmao commented Jan 10, 2018

加了一个output

def open_webbrowser_count(question,choices):
print('\n -- 题目+选项搜索结果计数法 -- Question:' + question + '\n')
#print('Question: ' + question + '\n')
if '不是' in question:
print('请注意此题为否定题,选计数最少的')

counts = []
for i in range(len(choices)):
    # 请求
    req = requests.get(url='http://www.baidu.com/s', params={'wd': question + choices[i]})
    content = req.text
    index = content.find('百度为您找到相关结果约') + 11
    content = content[index:]
    index = content.find('个')
    count = content[:index].replace(',', '')
    counts.append(count)
    #print(choices[i] + " : " + count)
    #print("\033[0;31m{0:^10}:{1:^10}\033[0m".format(choices[i],count))
    #print(counts)
output(choices, counts)

def count_base(question,choices):
print('\n -- 题目搜索结果包含选项词频计数法 -- Question:' + question + '\n')
# 请求
req = requests.get(url='http://www.baidu.com/s', params={'wd':question})
content = req.text
#print(content)
counts = []
#print('Question: ' + question + '\n')
if '不是' in question:
print('请注意此题为否定题,选计数最少的')
for i in range(len(choices)):
counts.append(content.count(choices[i]))
#print((choices[i], + " : " + str(counts[i]))
#print("\033[0;31m{0:^10}:{1:^10}\033[0m".format(choices[i], str(counts[i])))
output(choices, counts)

def output(choices, counts):
counts = list(map(int, counts))
#print(choices, counts)

# 最可能的答案
index_max = counts.index(max(counts))

# 最不可能的答案
index_min = counts.index(min(counts))

if index_max == index_min:
    print("\033[1;31m此方法失效!\033[0m")
    return

for i in range(len(choices)):
    if i == index_max:
        # 绿色为最可能的答案
        print("\033[1;32m{0:^10} {1:^10}\033[0m".format(choices[i], counts[i]))
    elif i == index_min:
        # 红色为最不可能的答案
        print("\033[0;31m{0:^10}{1:^10}\033[0m".format(choices[i], counts[i]))
    else:
        print("{0:^10} {1:^10}".format(choices[i], counts[i]))
@asseywang
Copy link

觉得流程需要优化,现在执行步骤太多,搜索结果解析速度太慢了,跟不上真题节奏

@Skyexu
Copy link
Owner

Skyexu commented Jan 10, 2018

@heishanmao 谢谢,最大最小结果可能有重复,我之前就没写了,看一眼也差不多,更新的代码已采用

@Skyexu
Copy link
Owner

Skyexu commented Jan 10, 2018

@asseywang 可尝试最新代码

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants