| 16 | |
| 17 | |
| 18 | def main(): |
| 19 | if len(sys.argv) > 1: |
| 20 | keyword = " ".join(sys.argv[1:]) |
| 21 | else: |
| 22 | # if no keyword is entered, the script would search for the keyword |
| 23 | # copied in the clipboard |
| 24 | keyword = pyperclip.paste() |
| 25 | |
| 26 | res = requests.get("http://google.com/search?q=" + keyword) |
| 27 | res.raise_for_status() |
| 28 | soup = bs4.BeautifulSoup(res.text) |
| 29 | linkElems = soup.select(".r a") |
| 30 | numOpen = min(5, len(linkElems)) |
| 31 | |
| 32 | for i in range(numOpen): |
| 33 | webbrowser.open("http://google.com" + linkElems[i].get("href")) |
| 34 | |
| 35 | |
| 36 | if __name__ == "__main__": |