11.6项目 "I'm Feeling Lucky"
#! python3
import requests
import bs4
import webbrowser
import sys
print('Baiduing...')
res = requests.get('http://www.baidu.com/s?wd=' + ' '.join(sys.argv[1:]))
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, features='html5lib')
link_elems = soup.select('.t a')
number_opens = min(5, len(link_elems))
for i in range(number_opens):
webbrowser.open(link_elems[i].get('href'))
程序运行结果
PS D:\My Documents\Python\Scripts> python.exe .\luck.py python 教程 推荐
Baiduing...
PS D:\My Documents\Python\Scripts>
捕获.PNG
网友评论