loop in python code to avoid repetition and reduce the code
<p>i would like create a loop and continue the script as long as data is not none.
I'm bad with loops. </p>

<pre><code>import requests
import re
import urllib

import json

url = 'http://bla/bla.html'


headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:42.0) Gecko/20100101 Firefox/42.0 Iceweasel/42.0', 'Referer': ''}

r1 = requests.get(url, headers=headers)

data_a = re.findall('data-a="(.*?)"', r1.text)[0]

data_a = urllib.unquote_plus(data_a)

data_a ={'p': data_a}

r2 = requests.post('http://bla/bla/get', headers=headers, data=data_a)

jdata = json.loads(r2.text)
data = jdata["data"]

newa = re.findall('newa": "(.*?)"', r2.text)[0]
newa = urllib.unquote_plus(newa)
newa ={'p': newa}

if data == None:

   print r1.text
else:

   r3 = requests.post('http://bla/bla/get', headers=headers, data=newa)
   jdata = json.loads(r3.text)
   data1 = jdata["data"]
   newa = re.findall('newa": "(.*?)"', r3.text)[0]
   newa = urllib.unquote_plus(newa)
   newa ={'p': newa}
   if data1 == None:

     print r1.text + data

   else:

     r4 = requests.post('http://bla/bla/get', headers=headers, data=newa)

     jdata = json.loads(r4.text)
     data2 = jdata["data"]

     newa = re.findall('newa": "(.*?)"', r4.text)[0]
     newa = urllib.unquote_plus(newa)
     newa ={'p': newa}

     if data2 == None:


        print r1.text + data + data1

     else:
        ...............            
</code></pre>

<p>I suppose need 'for i in' but I still do not understand how it works.<br>
the hardest thing for me is the story of addition r1.text + data + data1 .....<br>
really thanks for your help :)</p>

<p>ok i have the solution with an user in a forum, Now it's more clear</p>

<pre><code>url = 'http://bla/bla.html'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:42.0) Gecko/20100101 Firefox/42.0 Iceweasel/42.0', 'Referer': ''}
r = requests.get(url, headers=headers).text
data_a = urllib.unquote_plus(re.findall('data-a="(.*?)"', r)[0])
#print data_a

data_a ={'p': data_a}
nr = requests.post('http://bla/bla/get/', headers=headers, data=data_a).text
jdata = json.loads(nr)
data = jdata["data"]
#print data
while data != None :
  r=r+data
  newa = re.findall('newa": "(.*?)"', nr)[0]
  newa = urllib.unquote_plus(newa)
  newa ={'p': newa}
  nr = requests.post('http://bla/bla/get/', headers=headers, data=newa).text
  jdata = json.loads(nr)
  data = jdata["data"]

else:
  print r
</code></pre>

python loops