字符串格式 - 元组索引超出范围

时间:2015-07-30 16:10:15

标签: python string formatting

我正在尝试将变量放入长字符串,但它会引发错误。我已经尝试过%s符号,现在我试图将.format()符号放在那里,但它也不起作用。

你能告诉我这是什么问题吗?是因为字符串在多行上?

    num = 5
    r=requests.post("http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{0}'.html".format(str(num)), headers=mLib.firebug_headers_to_dict("""POST /kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{1}'.html HTTP/1.1
    Host: www.quoka.de
    User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{2}'.html
    Cookie: QSESSID=cs9djh8q8c6mjgsme85s4mf7iq24pqrthag630ar6po9fp078e20; PARTNER=VIEW%02quoka%01COOKIEBEGIN%021438270171; QUUHS=QPV%027%01QABAFS%02A%01ARYSEARCHODER%02%7B%22search1%22%3A%22nachhilfe%22%7D; __utma=195481459.415565446.1438270093.1438270093.1438270093.1; __utmb=195481459.22.8.1438270093; __utmc=195481459; __utmz=195481459.1438270093.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1; __utmt_t2=1; POPUPCHECK=1438356493224; axd=100086901728180087; __gads=ID=92bfc541911a1c81:T=1438270098:S=ALNI_MYuEdhQnu7sWAfK-fyKf1Ej93_9KA; crtg_rta=; OX_sd=5; OX_plg=wmp|pm; rsi_segs=L11278_10123|L11278_10571|L11278_11639|F12351_10001|F12351_0; PURESADSCL=done
    Connection: keep-alive
    """.format(str(num),str(num-1))))

ERROR:

 """.format(str(num),str(num-1))))
IndexError: tuple index out of range

2 个答案:

答案 0 :(得分:0)

您的格式字符串从1开始编号,而不是0.您只为插槽0和1提供2个值,但模板需要编号为1和2的参数。

请注意,此处有两个单独的字符串。第一个字符串使用{0},第二个字符串使用{1}{2}。将这些重新编号为{0}{1}

重申一下,第一个字符串很好:

"http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{0}'.html".format(str(num))

但你必须在第二个字符串中重新开始编号。在那个字符串中你有

POST /kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{1}'.html HTTP/1.1

Referer: http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{2}'.html

只需使用

POST /kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{0}'.html HTTP/1.1

Referer: http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{1}'.html

请注意,那些'..'单引号是您正在生成的字符串的一部分。我刚测试了网站,那里的网址没有使用这些引号。你应该删除它们。您也不必在所有内容上调用str(),模板会为您处理:

num = 5
r = requests.post(
    "http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_{0}.html".format(num),
    headers=mLib.firebug_headers_to_dict("""\
POST /kleinanzeigen/nachhilfe/cat_0_ct_0_page_{0}.html HTTP/1.1
Host: www.quoka.de
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_{1}.html
Cookie: QSESSID=cs9djh8q8c6mjgsme85s4mf7iq24pqrthag630ar6po9fp078e20; PARTNER=VIEW%02quoka%01COOKIEBEGIN%021438270171; QUUHS=QPV%027%01QABAFS%02A%01ARYSEARCHODER%02%7B%22search1%22%3A%22nachhilfe%22%7D; __utma=195481459.415565446.1438270093.1438270093.1438270093.1; __utmb=195481459.22.8.1438270093; __utmc=195481459; __utmz=195481459.1438270093.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1; __utmt_t2=1; POPUPCHECK=1438356493224; axd=100086901728180087; __gads=ID=92bfc541911a1c81:T=1438270098:S=ALNI_MYuEdhQnu7sWAfK-fyKf1Ej93_9KA; crtg_rta=; OX_sd=5; OX_plg=wmp|pm; rsi_segs=L11278_10123|L11278_10571|L11278_11639|F12351_10001|F12351_0; PURESADSCL=done
Connection: keep-alive
""".format(num, num - 1)
    ))

此外,我强烈怀疑您需要包含POST <path> HTTP/1.1行。 HostAccept-EncodingConnection标题将由requests处理,如果您要使用Session()对象,Cookie也将使用session = requests.Session() url = 'http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_{0}.html' num = 5 r = session.post( url.format(num), headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0', 'Referer': url.format(num - 1), } ) 。尽量不要明确设置所有这些标头。将标题设置为字典可能更具可读性:

POST

最后但并非最不重要的是,您确定需要在此处执行File "D:\test_for_nose.py", line 37, in __init__ conf = local_config.ConfigLoader("jefne_system_conf") File "build\bdist.win32\egg\testframework\configurator\configurator.py", line 184, in __init__ File "build\bdist.win32\egg\testframework\configurator\configurator.py", line 197, in _create_dependencies File "build\bdist.win32\egg\testframework\Engines\sshengine.py", line 137, in make_ssh_engine File "build\bdist.win32\egg\testframework\Engines\sshengine.py", line 114, in create_ssh_session_obj_from_hostname File "C:\Python27\lib\site-packages\paramiko-1.11.0-py2.7.egg\paramiko\client.py", line 342, in connect self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys) File "C:\Python27\lib\site-packages\paramiko-1.11.0-py2.7.egg\paramiko\client.py", line 524, in _auth self._transport.auth_password(username, password) File "C:\Python27\lib\site-packages\paramiko-1.11.0-py2.7.egg\paramiko\transport.py", line 1183, in auth_password return self.auth_handler.wait_for_response(my_event) File "C:\Python27\lib\site-packages\paramiko-1.11.0-py2.7.egg\paramiko\auth_handler.py", line 158, in wait_for_response event.wait(0.1) File "C:\Python27\lib\threading.py", line 618, in wait self.__cond.wait(timeout) File "C:\Python27\lib\threading.py", line 358, in wait _sleep(delay) KeyboardInterrupt 吗?你没有发布任何东西,GET也可以正常工作。

答案 1 :(得分:0)

在第二个多行字符串中,您将索引为 - {1}{2} -

_page_'{1}'.html

_0_page_'{2}'.html

但是你只给出两个变量作为format()函数的参数。

这就是为什么当格式函数试图访问{2}的变量(这是第三个参数,因为它的索引为0)时会出错。

您应将其定义为{0}{1}

示例 -

r=requests.post("http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{0}'.html".format(str(num)), headers=mLib.firebug_headers_to_dict("""POST /kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{0}'.html HTTP/1.1
    Host: www.quoka.de
    User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://www.quoka.de/kleinanzeigen/nachhilfe/cat_0_ct_0_page_'{1}'.html
    Cookie: QSESSID=cs9djh8q8c6mjgsme85s4mf7iq24pqrthag630ar6po9fp078e20; PARTNER=VIEW%02quoka%01COOKIEBEGIN%021438270171; QUUHS=QPV%027%01QABAFS%02A%01ARYSEARCHODER%02%7B%22search1%22%3A%22nachhilfe%22%7D; __utma=195481459.415565446.1438270093.1438270093.1438270093.1; __utmb=195481459.22.8.1438270093; __utmc=195481459; __utmz=195481459.1438270093.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1; __utmt_t2=1; POPUPCHECK=1438356493224; axd=100086901728180087; __gads=ID=92bfc541911a1c81:T=1438270098:S=ALNI_MYuEdhQnu7sWAfK-fyKf1Ej93_9KA; crtg_rta=; OX_sd=5; OX_plg=wmp|pm; rsi_segs=L11278_10123|L11278_10571|L11278_11639|F12351_10001|F12351_0; PURESADSCL=done
    Connection: keep-alive
    """.format(str(num),str(num-1))))