webbrowser
:::info
使用webbrowser.open()函数打开网址并进行搜索
搜索命令来自命令行参数或剪贴板
:::
import webbrowser, sys, pyperclipif len(sys.argv) > 1:# Get address from command line.address = ' '.join(sys.argv[1:])else:# Get address from clipboard.address = pyperclip.paste()# webbrowser.open('https://cn.bing.com/ditu/Default.aspx?q=' + address)#copy this to pasteboard and run this programme beauty girlwebbrowser.open('https://www.google.com.hk/search?q=' + address)
下载并保存文件
:::info 下载RemeoAndJuliet;保存到硬盘;打印文件路径 :::
import requests, osres = requests.get('http://www.gutenberg.org/cache/epub/1112/pg1112.txt')#res = requests.get('http://inventwithpython.com/page_that_does_not_exist')try:res.raise_for_status()playFile = open('RemeoAndJuliet.txt', 'wb')for chunk in res.iter_content(100000):playFile.write(chunk)playFile.close()print("file save ok!")print(f"saved path:{os.path.abspath('RemeoAndJuliet.txt')}")except Exception as exc:print('There was a problem: %s' % (exc))
