首页 > 代码库 > 11.6 项目: “I’m Feeling Lucky”百度 查找

11.6 项目: “I’m Feeling Lucky”百度 查找

 1 #! python3
 2 # lucky.py - Opens several Google search results.
 3 import requests, sys, webbrowser,logging
 4 from bs4 import BeautifulSoup
 5 logging.disable(logging.CRITICAL)
 6 logging.basicConfig(level=logging.DEBUG, format=%(asctime)s - %(levelname)s - %(message)s)
 7 print(Baiduing...) # display text while downloading the Baidu page
 8 logging.debug(sys.argv[1:])
 9 res = requests.get(http://www.baidu.com/s?wd= + ‘‘.join(sys.argv[1:]))
10 res.encoding = utf-8
11 #logging.debug(res)^
12 res.raise_for_status()
13 # TODO: Retrieve top search result links.
14 soup = BeautifulSoup(res.text, "lxml")
15 #logging.debug(soup)
16 # TODO: Open a browser tab for each result.
17 linkElems = soup.select(div.result h3.t > a)
18 numOpen = min(8, len(linkElems))
19 for i in range(numOpen):
20   webbrowser.open(linkElems[i].get(href))

改成百度了,你懂的。

11.6 项目: “I’m Feeling Lucky”百度 查找