我想创建一个循环并继续脚本,只要数据不是无。 我对循环不好。
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:
...............
我想我需要'但我仍然不明白它是如何工作的。
对我来说最难的是添加r1.text + data + data1 .....的故事
非常感谢你的帮助:)。
答案 0 :(得分:0)
好的我在论坛中有一个用户的解决方案,现在它更清楚了
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