简体   繁体   English

sys.argv列表索引超出范围错误

[英]sys.argv list index out of range error

I am trying to run this script that grabs rss feeds on the environment "Thonny" but I just keep receiving this error of "IndexError: List index out of range" 我正在尝试运行在“ Thonny”环境中获取rss feed的脚本,但是我一直收到以下错误:“ IndexError:列表索引超出范围”

Traceback (most recent call last):
File "C:\Users\uri\rssfeedfour.py", line 11, in <module>
url = sys.argv[1]
IndexError: list index out of range

How do I resolve this to keep from getting this error over and over again. 我如何解决此问题,以免一遍又一遍地出现此错误。 Im not sure how to solve this as I am a beginner. 我不知道该如何解决这个问题,因为我是新手。 Do I need to define it, if so how? 我是否需要定义它(如果这样)? or could I take it out and go a different direction? 还是我可以将其取出并朝另一个方向走? Here is the code. 这是代码。

import feedparser
import time
from subprocess import check_output
import sys

#feed_name = 'TRIBUNE'
#url = 'http://chicagotribune.feedsportal.com/c/34253/f/622872/index.rss'

feed_name = sys.argv[1]
url = sys.argv[2]

db = 'http://feeds.feedburner.com/TheHackersNews'
limit = 12 * 3600 * 1000

current_time_millis = lambda: int(round(time.time() * 1000))
current_timestamp = current_time_millis()

def post_is_in_db(title):
    with open(db, 'r') as database:
        for line in database:
            if title in line:
                return True
    return False


def post_is_in_db_with_old_timestamp(title):
    with open(db, 'r') as database:
        for line in database:
            if title in line:
                ts_as_string = line.split('|', 1)[1]
                ts = long(ts_as_string)
                if current_timestamp - ts > limit:
                    return True
    return False

#
# get the feed data from the url
#
feed = feedparser.parse(url)

#
# figure out which posts to print
#
posts_to_print = []
posts_to_skip = []

for post in feed.entries:
    # if post is already in the database, skip it
    # TODO check the time
    title = post.title
    if post_is_in_db_with_old_timestamp(title):
        posts_to_skip.append(title)
    else:
        posts_to_print.append(title)

#
# add all the posts we're going to print to the database with the current timestamp
# (but only if they're not already in there)
#
f = open(db, 'a')
for title in posts_to_print:
    if not post_is_in_db(title):
        f.write(title + "|" + str(current_timestamp) + "\n")
f.close

#
# output all of the new posts
#
count = 1
blockcount = 1
for title in posts_to_print:
    if count % 5 == 1:
        print("\n" + time.strftime("%a, %b %d %I:%M %p") + '  ((( ' + feed_name + ' - ' + str(blockcount) + ' )))')
        print("-----------------------------------------\n")
        blockcount += 1
    print(title + "\n")
    count += 1

sys.argv is a list in Python, which contains the command-line arguments passed to the script. sys.argv是Python中的列表,其中包含传递给脚本的命令行参数。 sys.argv[0] contains the name of the script, sys.argv[1] contains the first argument and so on. sys.argv[0]包含脚本的名称, sys.argv[1]包含第一个参数,依此类推。

To prevent this error, you need to give command line arguments when starting the script. 为防止此错误,启动脚本时需要提供命令行参数。 For example, you can start this script without any errors by 例如,您可以通过以下方式启动此脚本,而不会出现任何错误:

python rssfeedfour.py TRIBUNE http://chicagotribune.feedsportal.com/c/34253/f/622872/index.rss

You can also modify the script so that it works using the default arguments if you don't provide any command line arguments. 您也可以修改脚本,以便在不提供任何命令行参数的情况下使用默认参数来工作。

try:
    feed_name = sys.argv[1]
except IndexError:
    feed_name = 'TRIBUNE'

try: 
    url = sys.argv[2]
except IndexError:
    url = 'http://chicagotribune.feedsportal.com/c/34253/f/622872/index.rss'

You can learn more about handling errors here . 您可以在此处了解有关处理错误的更多信息。

Although it is much more convenient to use argparse library. 尽管使用argparse库要方便得多。

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

相关问题 Sys.argv python错误列表索引超出范围 - Sys.argv python error list index out of range IndexError:列表索引超出范围:sys.argv[1] 超出范围 - IndexError: list index out of range: sys.argv[1] out of range driver.get(sys.argv[1]) 在 PC 上正常工作,而在其他 PC 上工作正常(sys.argv[1],IndexError: list index out of range 错误) - driver.get(sys.argv[1]) working fine on a PC and not on the other (sys.argv[1], IndexError: list index out of range error) python sys.argv[1] 列表索引超出范围 - python sys.argv[1] list index out of range sys.argv[1] 索引错误:列表索引超出范围 - sys.argv[1] IndexError: list index out of range 使用 sys.argv[1] 时“列表索引超出范围” - “list index out of range” when using sys.argv[1] Python sys.argv [1]索引超出范围 - Python sys.argv[1] index out of range 当我运行此命令“cascPath = sys.argv[1]”时,我收到错误 IndexError: list index out of range - When i run this command " cascPath = sys.argv[1] " i get error IndexError: list index out of range 读取 csv 文件错误:sys.argv[1], IndexError: list index out of range - Reading in csv file error: sys.argv[1], IndexError: list index out of range 错误:文件“ xml_parser.py”的第5行 <module> out_file = sys.argv [2] IndexError:列表索引超出范围 - Error: File “xml_parser.py”, line 5, in <module> out_file = sys.argv[2] IndexError: list index out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM