美文网首页Python
《Python编程快速上手—让繁琐工作自动化》第11章实践项目答

《Python编程快速上手—让繁琐工作自动化》第11章实践项目答

作者: simon_xu0559 | 来源:发表于2019-11-03 19:45 被阅读0次

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

相关文章

网友评论

    本文标题:《Python编程快速上手—让繁琐工作自动化》第11章实践项目答

    本文链接:https://www.haomeiwen.com/subject/hvslbctx.html