我正在尝试下载剪贴板中包含的 URL,但我找不到阻止反复下载同一页面的方法。这是我尝试过的,但我得到错误TypeError: 'int' object has no attribute '__getitem__'
这是什么意思?它说错误在第 13 行,这是它检查 URL 是否有效的地方。
import time
import os
import urllib
basename = "page"
extension = ".html"
count=0
old_url = ""
while(1):
time.sleep(1) #check clipboard every second
clipboard = os.system("pbpaste") # get contents of clipboard
if clipboard[:4] == "http" and clipboard != old_url: # check if valid URL and is diffrent
while os.path.exists(basename+str(count)+extension): # Create new name
count=count+1
old_url = clipboard
name=basename+str(count)+extension
data=urllib.urlopen(clipboard).read() #get page data
file(name, "wb").write(data) # write to file