Pythonは簡単な索引の並べ替えと検索機能を実現します。


今日、私の授業でインデックスの並べ替えと検索を学びました。Pythonを使って実現しましょう。ちょっと面白いと思ったら皆さんと一緒に波を分けます。
コードは下図の通りです

import requests
import re

def News_Spider():#      
    url = 'https://news.sina.com.cn/'#url  ,    
    headers = {#   
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
    }
    response = requests.get(url,headers,verify=False)#  https,  verify=False
    response.encoding='utf-8'#    
    html = response.text#       
    #print(html)#     
    reg = 'target="_blank">(.*?)</a>'#    
    content = re.findall(reg,html)#         
    ls = []#       
    for c in content:
        if '<' in c:
            continue
        else:
            if len(c) > 6 and '   ' not in c:
                #print(c)
                ls.append(c)
            else:
                continue
    docu_set = {}#      
    for l in range(len(ls)):
        docu_set['d{}'.format(l+1)] = ls[l]#     , 1  
    return docu_set

def change_set():
    all_words = []#           
    docu_set = News_Spider()
    for i in docu_set.values():
        cut = i.split()#  
        all_words.extend(cut)#    
    set_all_words = set(all_words)
    return set_all_words
    #print(set_all_words)

def reverse_index():
    invert_index = dict()#     
    set_all_words = change_set()#         
    docu_set = News_Spider()
    for b in set_all_words:
        temp = []
        for k in docu_set.keys():
            field = docu_set[k]
            split_field = field.split()
            if b in split_field:
                temp.append(k)
        invert_index[b] = temp
    print(invert_index)
    return invert_index

def Select():
    docu_set = News_Spider()
    invert_index = reverse_index()
    news = []
    # for i in invert_index:
    #     print(invert_index[i])
    while True:
        Find = str(input('       :'))
        if Find == '   ':
            break
        for Contetnt in invert_index:#      
            if Find in Contetnt:#           
                Result = invert_index[Contetnt]#             
                #print(Result)
                for r in Result:#      
                    if r in docu_set.keys():#       
                        news.append(docu_set[r])#      docu_set  
                        print(docu_set[r])#        
                    else:
                        continue
            else:
                if Find not in Contetnt:
                    news.append('   ,        !!')
        #news = set(news)
        for n in news:
            if '   ' in n:
                print(n)
                break
            else:
                print(n)

def main_function():#       
    News_Spider()
    change_set()
    reverse_index()
    Select()

if __name__ == '__main__':#    
    main_function()
運転結果は下図のようになります。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
コードの解釈についてコメントに書きました。
ここでPythonについて簡単な索引の並べ替えと検索機能を実現する記事を紹介します。より多くの関連pythonはインデックスの並べ替えと検索内容を実現します。以前の記事を検索してください。または下記の関連記事を引き続きご覧ください。これからもよろしくお願いします。