为什么会出现“SyntaxError:invalid syntax”

问题描述 投票:0回答:1

错误信息:

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

文件“// rschfs1x / users / 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'] = "[email protected]"
     msg['To'] = "[email protected]"

     # Credentials (if needed)
     username = '[email protected]'
     password = ''
     # The actual mail send
     server = smtplib.SMTP('smtp.gmail.com:587')
     server.starttls()
     server.login(username,password)
     server.sendmail("hello", "[email protected]", alertmessage)
     server.sendmail("hello", "[email protected]", alertmessage)
     #server.sendmail("hello", "[email protected]", alertmessage)    
     server.sendmail("hello", "[email protected]", alertmessage)    

    '''
    Shanjun Li <[email protected]>,
    Panle Barwick <[email protected]>,
    Jing Qian <[email protected]>    
    '''        
    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)
python syntax
1个回答
6
投票

这一行:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.