51Testing软件测试论坛

标题: 《Python 编程快速上手》page202-百度版本 [打印本页]

作者: lvchong2020    时间: 2020-11-27 21:41
标题: 《Python 编程快速上手》page202-百度版本
#! /usr/bin/env python3
# lucky.py - Open several Baidu search results.

import requests, sys, webbrowser, bs4

print('Baiduing...') # display text while downloading the Baidu page
res = requests.get('http://www.baidu.com/s?wd='+ ' '.join(sys.argv[1:]))
res.raise_for_status()
# Retrieve top search result links.
soup = bs4.BeautifulSoup(res.text)
# Open a browser tab for each result.
linkElems = soup.select('.t a')
numOpen = min(5, len(linkElems))

for i in range(numOpen):
    webbrowser.open(linkElems[i].get('href'))




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2