Python添加到中的现有变量
代码的快速示例:Python添加到中的现有变量,python,python-requests,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://
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
到
通过每次在代码中收到异常时添加+1,您可以在循环中将POST设置为等于0。将post=0部分移动到循环之前,您之前尝试的应该会起作用。这不会起作用,因为您在下一次
for
循环中立即设置了post=0
。在循环的之前定义post=0
。旁注:我发现“post”是一个非常令人困惑的名字,用来跟踪GET请求的错误。是你,还是只是有人在做同样的任务?天哪,谢谢@roganjosh//试过的post=post+1(不起作用)
什么叫“不起作用”?当你尝试这个的时候发生了什么?你到底想发生什么?@blogbeard nope他们的代码和我的略有不同,事实上非常不同:)
post = 1