自动打开搜狗搜索的前五个链接

#! python3
# lucky.py - Opens several sogou search results.
"""
Created on Sat Feb 24 10:07:52 2018


@author: He Yuanwei
"""


import requests, webbrowser, bs4, sys
print('搜狗ing...') # display text while downloading the baidu page
# str = "-";
# seq = ("a", "b", "c"); # 字符串序列
# print str.join( seq );
# 结果  a-b-c


res = requests.get('https://www.sogou.com/web?query=' + ' '.join(sys.argv[1:]))
res.raise_for_status()
res.text
# TODO: Retrieve top search result links.
# 使用BeautifulSoup解析这段代码,'lxml'是解析器
soup = bs4.BeautifulSoup(res.text, 'lxml')
# TODO: Open a browser tab for each result.
linkElems = soup.select('.vrTitle a')
numOpen = min(5, len(linkElems))
for i in range(numOpen):
    # dict = {'Name': 'Zara', 'Age': 27}
    # print "Value : %s" %  dict.get('Age')
    # 结果  Value : 27
    
    # Python 字典(Dictionary) get()方法 | 菜鸟教程
    webbrowser.open(linkElems[i].get('href'))    

你可能感兴趣的:(自动打开搜狗搜索的前五个链接)