简体   繁体   English

错误的不存在的行Python

[英]Error on non-existent line Python

I don't know what to do, I have a 39 line Python script and it gives me an error on line 40! 我不知道该怎么办,我有一个39行的Python脚本,它在40行给我一个错误! :( Error: :(错误:

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>

And here's my Python code: 这是我的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")

If you can help me, please do. 如果可以帮助我,请这样做。 I've spent many hours on this so far, and when it's finally ready, I get this. 到目前为止,我已经花了很多时间来做,终于准备好了,我明白了。 Thanks in advance! 提前致谢!

除了尝试,您没有匹配项

try之后和该行之前应该有一个except

execfile("nextrunsSoup.py")

因此,文件的正文被包裹在try: ,而您的except:或在哪里finally:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM