为什么会出现#34; SyntaxError:无效的语法"

时间:2015-05-15 19:55:56

标签: python syntax

错误讯息:

  

runfile(' // rschfs1x / userrs / xd73_RS / Documents / Python Scripts / test_515.py',wdir =' // rschfs1x / userrs / xd73_RS / Documents / Python Scripts' )

     

文件" // rschfs1x / userrs / xd73_RS / Documents / Python Scripts / test_515.py",第120行

if __name__ == "__main__":

                        ^
     

SyntaxError:语法无效

真的不确定为什么会出现这个问题

def sendemail(alertmessage):
     msg = MIMEText(alertmessage)
     msg['Subject'] = 'Patent Data Error Message' 
     msg['From'] = "patentcornell@gmail.com"
     msg['To'] = "patentcornell@gmail.com"

     # Credentials (if needed)
     username = 'patentcornell@gmail.com'
     password = ''
     # The actual mail send
     server = smtplib.SMTP('smtp.gmail.com:587')
     server.starttls()
     server.login(username,password)
     server.sendmail("hello", "xd73@cornell.edu", alertmessage)
     server.sendmail("hello", "jq58@cornell.edu", alertmessage)
     #server.sendmail("hello", "sl2448@cornell.edu", alertmessage)    
     server.sendmail("hello", "patentcornell@gmail.com", alertmessage)    

    '''
    Shanjun Li <sl2448@cornell.edu>,
    Panle Barwick <pjb298@cornell.edu>,
    Jing Qian <jq58@cornell.edu>    
    '''        
    server.quit()

 def main(year = 1985, starting_page = 2, filepath = ""):
    time1 = time.time()
    print "start timing: " + str(time1)
    driver = webdriver.Firefox()
    driver.get("http://epub.sipo.gov.cn/")
    elem = driver.find_element_by_id("soso_text")
    f = open( filepath + "year_test_" + str(year), "wb")
    flog = open(filepath + "log_" + str(year), "wb")
    driver.get("http://epub.sipo.gov.cn/")
    elem = driver.find_element_by_id("soso_text") 
    elem.send_keys(str(year))
    elem.send_keys(Keys.RETURN)
    content = driver.page_source.encode('utf-8')    
    for uchar in content:
        f.write(bytearray([ord(uchar)]))
    flog.write(str(year) + " " + str(1) + "\n")
    case = 1
    nextpage =        driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[4]/a[7]")
    turnto =      driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[4]/span")
    print "hello 0"
    print nextpage.get_attribute("innerHTML")
    totalnum = int(nextpage.get_attribute("innerHTML"))
    print "totalnum: " + str(totalnum)
    #try: 
    # from which page we start downloading, the starting page
    for i in range(starting_page, totalnum + 1):
               timeinterval_1 = time.time()
               print str(year) + " page: " + str(i)
               #turnto =                 driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[4]/span")
               turnto = driver.find_element_by_id('pn')
               turnto.send_keys(str(i))
               turnto.send_keys(Keys.ENTER)
                    #turnto.submit()
               content = driver.page_source.encode('utf-8')
               # test file writing speed

               time_file_start = time.time()
               for uchar in content:
                   f.write(bytearray([ord(uchar)]))
               f.write("\n")
               #robust Check
               print "interval: " + str(timeinterval_2 - timeinterval_1)
               if timeinterval_2 - timeinterval_1 > 60:
                   flog.write("lost: " + str(year) + " page: " + str(i) + "\n")
                   print "too long to load " + str(year) + " " + str(i)
                   continue
               else:
                   flog.write(str(year) + " " + str(i) + "\n")
                   continue
    #except ValueError as err:
    print(err.args)
    sendmail("xd73_RS: " + err.args + " " + str(time.time())     


if __name__ == "__main__":
    filepath = "U:/Documents/Python Scripts/test_data_515/"
    #sendemail("test email function!")
    main(2010, 2, filepath)

1 个答案:

答案 0 :(得分:6)

这一行:

sendmail("xd73_RS: " + err.args + " " + str(time.time())

有无与伦比的括号。通常,如果在n行上发生语法错误,最好检查一下n-1行的语法。