2010-09-17 58 views
2

我試圖用Jython編譯Python程序和它拋出以下錯誤無法使用Jython

C:\jython2.2.1>jython test.py 
Traceback (innermost last): 
    (no code object) at line 0 
    File "test.py", line 30 
        html = html if html else download(url, user_agent).read() 
           ^
SyntaxError: invalid syntax 

下面是我的Python程序編譯Python程序。請讓我知道如何解決這個問題。


import sys 
import re 
import urllib2 
import urlparse 
from optparse import OptionParser 


# regular expression data for each website 
VIDEO_DATA = [ 
    ('youtube.com', '%7C(.*?videoplayback.*?)%2C'), 
    ('metacafe.com', '&mediaURL=(.*?)&'), 
] 
# default user agent to use when downloading 
USER_AGENT = 'pytube' 
# size of file to download 
CHUNK_SIZE = 1024 * 1024 



def scrape(url, html=None, user_agent=None, output=None): 
    """Scrape video location from given url. 

    Use html instead of downloading if passed. 
    Download file to output if passed. 
    Return url of video if found, else None 
    """ 
    netloc = urlparse.urlsplit(url).netloc 
    for domain, video_re in VIDEO_DATA: 
     if domain in netloc: 
      html = html if html else download(url, user_agent).read() 
      search = re.search(video_re, html) 
      if search: 
       flash_url = urllib2.unquote(search.group(1)) 
       if output: 
        print "Downloading flash to `%s'" % output, 
        #open(output, 'wb').write(download(flash_url, user_agent).read()) 
        req = download(flash_url, user_agent) 
       # testing with keyword in python 
        with open(output, 'wb') as fp: 
         chunk = True 
         while chunk: 
          chunk = req.read(CHUNK_SIZE) 
          if chunk: 
           fp.write(chunk) 
           #fp.flush() 
           print '.', 
           sys.stdout.flush() 
        print 
       return flash_url 
      else: 
       raise PyTubeException('Failed to locate video regular expression in downloaded HTML') 
    raise PyTubeException('URL did not match available domains') 


def download(url, user_agent=None): 
    """Download url and return data 
    """ 
    headers = {'User-Agent' : user_agent} 
    req = urllib2.Request(url, None, headers) 
    return urllib2.urlopen(req) 


class PyTubeException(Exception): 
    pass 



if __name__ == '__main__': 
    # parse command line options 
    parser = OptionParser(usage='usage: %prog, [-o <file.flv> -a <user_agent> -s -h] url') 
    parser.add_option('-o', '--output', dest='output', help='Output file to download flash file to. If this is not specified file will not be downloaded.') 
    parser.add_option('-s', '--sites', action='store_true', default=False, dest='sites', help='Display sites that pytube supports, then quit.') 
    parser.add_option('-a', '--agent', dest='user_agent', default=USER_AGENT, help='Set user-agent for downloads.') 
    options, args = parser.parse_args() 
    if options.sites: 
     print '\n'.join(domain for (domain, reg) in VIDEO_DATA) 
    else: 
     if args: 
      flash_url = scrape(args[0], user_agent=options.user_agent, output=options.output) 
      if flash_url: 
       print flash_url 
     else: 
      print 'Need to pass the url of the video you want to download' 
      parser.print_help() 

+0

替換此:\ jython2.2.1>的Jython test.py 回溯(由外向內) (無代碼對象)在第0行 文件「test.py」,第30行 html =(html)? 「HTML」:下載(網址,USER_AGENT).read() ^ 語法錯誤:不一致DEDENT語言 – JavaGeek 2010-09-17 10:50:51

回答

1

的Jython 2.2.1是(AFAIK)相當於CPython的2.2.1儘可能語法而言。導致問題的線使用稍後介紹的三元運算符。解決方案是用if語句替換它。

if not html: 
    html = download(url, user_agent).read() 

這應該照顧那個語法錯誤。還有一個with子句,您將需要替換。

with open(output, 'wb') as fp: 
    chunk = True 
    while chunk: 
     chunk = req.read(CHUNK_SIZE) 
     if chunk: 
      fp.write(chunk) 
      #fp.flush() 
      print '.', 
      sys.stdout.flush() 

您可以更改代碼如你所說那麼它顯示以下錯誤 ℃之後與

try: 
    fp = open(output, 'w') 
    chunk = True 
    while chunk: 
     chunk = req.read(CHUNK_SIZE) 
      if chunk: 
       fp.write(chunk) 
       #fp.flush() 
       print '.', 
       sys.stdout.flush() 
finally: 
    fp.close() 
+0

現在它拋出錯誤,如^ 語法錯誤:DEDENT語言不一致 C:\ jython2.2.1>的Jython test.py 回溯(最裏面的最後): (無碼對象)在行0 文件 「test.py」,行30 如果不是HTML:HTML =下載(網址,USER_AGENT).read() ^ 語法錯誤:DEDENT語言不一致 – JavaGeek 2010-09-17 10:55:59

+0

薩庫馬爾:在Python,縮進很重要。你沒有正確縮進某些東西。 – geoffspear 2010-09-17 10:58:59

+0

其實我是新來的蟒蛇,仍然試圖找出這有什麼問題..沒有運氣..但同樣的錯誤 – JavaGeek 2010-09-17 11:05:31