Python添加到现有变量中除外

时间:2018-01-23 23:44:49

标签: python python-requests

代码的快速示例:

import requests
import re

for pages in range(1,10):

    post = 0

    myfile = open('posts.txt')
    data=myfile.read().splitlines()
    poster=data[post]

    postersend = {'param1': poster}

    try:

        resp = requests.get('http://www.postexample.com/pages={}'.format(pages), params=postersend).text

    except:
        //change post from 0 to 1 using + 1
        // tried post = post + 1 ( didn't work )
        continue

所以我想做的就是

except:

被触发然后改变

post = 0

post = 1

每次收到异常时加上+ 1

1 个答案:

答案 0 :(得分:1)

在您的代码中,您在循环中将帖子设置为等于0。 循环之前将post = 0部分移动到,之前尝试过的工作应该有效。