菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
1534
0

python 写的搜索引擎

原创
05/13 14:22
阅读数 3002
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()

发表评论

0/200
1534 点赞
0 评论
收藏
为你推荐 换一批