webbrowser

:::info 使用webbrowser.open()函数打开网址并进行搜索
搜索命令来自命令行参数或剪贴板 :::

  1. import webbrowser, sys, pyperclip
  2. if len(sys.argv) > 1:
  3. # Get address from command line.
  4. address = ' '.join(sys.argv[1:])
  5. else:
  6. # Get address from clipboard.
  7. address = pyperclip.paste()
  8. # webbrowser.open('https://cn.bing.com/ditu/Default.aspx?q=' + address)
  9. #copy this to pasteboard and run this programme beauty girl
  10. webbrowser.open('https://www.google.com.hk/search?q=' + address)

下载并保存文件

:::info 下载RemeoAndJuliet;保存到硬盘;打印文件路径 :::

  1. import requests, os
  2. res = requests.get('http://www.gutenberg.org/cache/epub/1112/pg1112.txt')
  3. #res = requests.get('http://inventwithpython.com/page_that_does_not_exist')
  4. try:
  5. res.raise_for_status()
  6. playFile = open('RemeoAndJuliet.txt', 'wb')
  7. for chunk in res.iter_content(100000):
  8. playFile.write(chunk)
  9. playFile.close()
  10. print("file save ok!")
  11. print(f"saved path:{os.path.abspath('RemeoAndJuliet.txt')}")
  12. except Exception as exc:
  13. print('There was a problem: %s' % (exc))