python 寫的搜尋引擎

TNT_God發表於2019-08-31
import requests, re, time, webbrowser, codecs
print('==========搜尋引擎==========')
time.sleep(0.7)
headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 
 'Accept-Encoding':'gzip, deflate, sdch', 
 'Accept-Language':'zh-CN,zh;q=0.8', 
 'Cache-Control':'max-age=0', 
 'Connection':'keep-alive', 
 'Cookie':'SRCHD=AF=NOFORM; SRCHUID=V=2&GUID=E4CB65F3BD7F4EC7922E3642567A39EC&dmnchg=1; _EDGE_V=1; MUID=24CC781F18B266D70F9C758D199C670F; MUIDB=24CC781F18B266D70F9C758D199C670F; SRCHUSR=DOB=20190707&T=1562487393000; SNRHOP=I=&TS=; _EDGE_S=mkt=zh-cn&SID=2C85ED242A1D66051D4FE0B62B33673B; _SS=SID=2C85ED242A1D66051D4FE0B62B33673B&HV=1562490664; SRCHHPGUSR=CW=1089&CH=1742&DPR=1&UTC=480&WTS=63698084193&PR=3', 
 'DNT':'1', 
 'Host':'cn.bing.com', 
 'Upgrade-Insecure-Requests':'1', 
 'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'}
try:
    try:
        while True:
            web_ids = {}
            test = []
            search = input('輸入搜尋的內容:')
            page = [11, 21, 31, 41, 51]

            def catch():
                global webs
                id_times = 0
                for t in page:
                    url = 'https://cn.bing.com/search?q=' + search + '&qs=n&sp=-1&pq&sc=0-5&sk=&cvid=275512403280414F9363B7CDC7368CBD&first=' + str(t) + '&FORM=PERE'
                    text = requests.get(url, headers=headers).text
                    a1 = '<h2>(.*?)</h2>'
                    a2 = 'href="(.*?)"'
                    a3 = '>(.*?)</a>'
                    title = re.findall(a1, text)
                    for j in title:
                        id_times = id_times + 1
                        title = re.findall(a3, j)
                        webs = re.findall(a2, j)
                        web_ids[id_times] = webs
                        print(id_times, title[0])
                        print('網址:%s' % webs[0])
                        print()
                        if len(webs) > 0:
                            test.append(1)

                if len(test) > 0:
                    print('開啟網址(如需開啟多個網址,請用英文符號“,”,使用其它鍵預設不開啟任何網址)')
                    open_web = input('網址編號:').split(',')
                    if 'n' not in open_web:
                        for aweb in open_web:
                            if aweb.isdigit():
                                ty = int(aweb)
                                if ty in web_ids:
                                    web = web_ids[ty]
                                    webbrowser.open(web[0])

                    else:
                        print('沒有符合您的搜尋結果!')
                    print('==============================')

            catch()

    except KeyboardInterrupt:
        exit()

except requests.exceptions.ConnectionError:
    print('抱歉,網路出現了一點問題!')
    time.sleep(3)
    exit()
本作品採用《CC 協議》,轉載必須註明作者和本文連結

Derek

相關文章