我正在尝试自动将值输入到此网页中 http://predict.habhub.org/ 并下载按下运行预测按钮时生成的结果csv文件。 我希望能够多次执行此操作(初始参数的90种不同组合),我可以使for循环正常,但无法使用代码将此信息输入到网站中。
altitudes = [32000, 33000, 34000]
ascentRates = [3.0, 3.5, 4.0, 4.5, 5.0, 5.5]
descentRates = [11, 13, 15, 17, 19]
for altitude in altitudes:
for ascent in ascentRates:
for descent in descentRates:
print(altitude)
print(ascent)
print(descent)
print("")
然而,这可能非常简单,因为我的编程知识有限,我真的不知道从哪里开始。我试图用python(urllib / requests)做到这一点,但我没有到达任何地方。希望有人能指出我正确的方向。
答案 0 :(得分:0)
对于涉及HTTP和Python的任何事情,requests
模块是(可以说是唯一的)正确的选择。
在这种情况下,你需要" POST" (上传)您的数据到服务器(请参阅requests documentation)。
现在你必须找出什么样的数据" POSTed"哪里。例如,使用Chrome的网络检查器,您可以看到以下数据发布到http://predict.habhub.org/ajax.php?action=submitForm
:
launchsite=Other&lat=52.2135&lon=0.0964&initial_alt=0&hour=21&min=48&second=0&day=23&month=6&year=2017&ascent=5&burst=30000&drag=5&submit=Run+Prediction
您现在必须创建一个可以提供给requests.post()的有效负载字典。在此dict中,您可以将值调整为要发送到服务器的任何值(请注意,表单使用的标识符与样本数据不同:burst = altitude,drag = descentRate):
import requests
payload = {'launchsite': 'Other', 'lat': '52.2135', 'lon': '0.0964', 'initial_alt': '0', 'hour': '21', 'min': '48', 'second': '0', 'day': '23', 'month': '6', 'year': '2017', 'ascent': '5', 'burst': '30000', 'drag': '5', 'submit': 'Run+Prediction'}
url = 'http://predict.habhub.org/ajax.php?action=submitForm'
r = requests.post(url, payload)
如果一切顺利,你应该收到一些包含你的uuid的json数据,你可以按如下方式提取:
uuid = r.json()['uuid']
现在您可以下载并存储您的csv数据:
csv_url = 'http://predict.habhub.org/preds/{}/flight_path.csv'.format(uuid)
r = requests.get(csv_url)
with open('flight_path.csv', 'w') as f:
f.write(r.text)
答案 1 :(得分:0)
为了解决我的困境,我认识到了Jkm!
对于遇到类似问题的人来说,这是我的最终代码:
import requests
import json
#The date must be in the future for this to work!
launchSite = 'Other' #Don't change this line
latitude = 39.0063
longitude = -104.8841
launchAltitude = 2212
launchHour = 15 #For colorado add 6hrs to local time(MDT)
launchMinute = 0
launchSecond = 0
launchDay = 24
launchMonth = 6 #this must be a number(ie June = 6)
launchYear = 2017
ascentRates = [3.0, 3.5, 4.0, 4.5, 5.0, 5.5]
burstAltitudes = [32000, 33000, 34000]
descentRates = [11, 13, 15, 17, 19]
for burstAltitude in burstAltitudes:
for ascent in ascentRates:
for descent in descentRates:
print(burstAltitude)
print(ascent)
print(descent)
print("")
payload = {'launchsite': launchSite, 'lat': latitude, 'lon': longitude, 'initial_alt': launchAltitude, 'hour': launchHour,
'min': launchMinute, 'second': launchSecond, 'day': launchDay, 'month': launchMonth, 'year': launchYear, 'ascent': ascent,
'burst': burstAltitude, 'drag': descent, 'submit': 'Run+Prediction'}
url = 'http://predict.habhub.org/ajax.php?action=submitForm'
r = requests.post(url, payload)
print(r.text)
uuid = r.json()['uuid']
csv_url = 'http://predict.habhub.org/preds/{}/flight_path.csv'.format(uuid)
r = requests.get(csv_url)
name = 'burst=' + str(burstAltitude) +' ascent=' + str(ascent) + ' descent=' + str(descent)
with open('predicted_%s.csv' % name, 'w') as f:
f.write(r.text)