我不知道該怎麼做,我有一個39行的Python腳本,它在第40行給我一個錯誤! :(錯誤:不存在的行上的錯誤Python
Traceback (most recent call last):
File "C:\Mass Storage\pythonscripts\Internet\execute.py", line 2, in <module>
execfile("firstrunSoup.py")
File "firstrunSoup.py", line 40
^
SyntaxError: invalid syntax
C:\Mass Storage\pythonscripts\Internet>
這是我的Python代碼:
###firstrunSoup.py###
FILE = open("startURL","r") #Grab from
stURL = FILE.read() #Read first line
FILE.close() #Close
file2save = "index.txt" #File to save URLs to
jscriptV = "not"
try:
#Returns true/false for absolute
def is_absolute(url):
return bool(urlparse.urlparse(url).scheme)
#Imports
import urllib2,sys,time,re,urlparse
from bs4 import BeautifulSoup
cpURL = urllib2.urlopen(stURL) #Human-readable to computer-usable
soup = BeautifulSoup(cpURL) #Defines soup
FILE = open(file2save,"a")
for link in soup.find_all('a'): #Find all anchor tags
outPut = ""
checkVar = link.get('href') #Puts href into string
if (checkVar is not None) and (checkVar != ""): #Checks if defined
if len(checkVar) > 11: #Check if longer than 11 characters
if checkVar[:11] != "javascript:": #Check if first 11 are "javascript:"
if checkVar[:7] != "mailto:": #Check if first 7 are "mailto:"
jscriptV = "not"
else: jscriptV = ""
else: jscriptV = ""
if checkVar != "#" and checkVar != "/":
if jscriptV == "not":
if checkVar is not None: #Checks if defined
if is_absolute(checkVar): outPut = checkVar.split("#")[0]
else: outPut = urlparse.urljoin(stURL,checkVar).split("#")[0]
if outPut != "":
print outPut
FILE.write(outPut + "\r\n")
FILE.close()
execfile("nextrunsSoup.py")
如果你能幫助我,請不要我到目前爲止已經花了很多時間在這,而當它終於準備好了,我提前得到這個。謝謝!
非常感謝之前應該有一個
except
塊,大家好! :) – Andrey