带参数

时间:2017-04-15 06:15:32

标签: python-2.7 python-requests

我是Python的新手,我想知道你是否可以请我帮我解决这个问题。 我正在使用带有请求模块的Python 2.7,一切都很顺利,除了我收到的数据不正确。

import requests

URL = 'https://www.cashbackforex.com/DesktopModules/Chart/Candles.ashx'
params = {'cp': '03/10/2017 7:30', 'int': '1', 'pair': '1', 'candles': '50', 'timezone': '12', 'candlestype': '0', 'prevTzld': '12', 'inst': '10351841'}

response = requests.get(URL, params=params)
print response.content

我从上面的代码收到的数据样本是:

{"Data":"03/10/2017 07:06,1.059010000,1.059220000,1.058980000,1.059190000\n03/10/2017 ...

虽然它应该是:

{"Data":"03/10/2017 07:06,1.060450000,1.060540000,1.060410000,1.060430000\n03/10/2017 ...

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我建议使用request替代urllib2而不是import urllib2 import urllib params = urllib.urlencode({"cp":"03/10/2017 07:30", "int":1, "pair":1, "candles":50,"timezone":12, "candlestype":0, "prevTzId":12, "inst":10351841}) target = "https://www.cashbackforex.com/DesktopModules/Chart/Candles.ashx?" body = urllib2.urlopen(target + params) print body.read() 这是一个例子。我已经尝试了它并成功检索到了所需的输出。

{"Data":"03/10/2017 07:06,1.060450000,1.060540000,1.060410000,1.060430000\n03/10/2017 07:07,1.06043
0000,1.060520000,1.060250000,1.060320000\n03/10/2017 07:08,1.060320000,1.060410000,1.060290000,1.06
0390000\n03/10/2017 07:09,1.060390000,1.060580000,1.060360000,1.060560000\n03/10/2017 07:10,1.06056
0000,1.060590000,1.060460000,1.060480000\n03/10/2017 07:11,1.060480000,1.060610000,1.060420000,1.06
0600000\n03/10/2017 07:12,1.060600000,1.060810000,1.060570000,1.060770000\n03/10/2017 07:13,1.06077
0000,1.060770000,1.060540000,1.060600000\n03/10/2017 07:14,1.060600000,1.060760000,1.060540000,1.06
0760000\n03/10/2017 07:15,1.060760000,1.060870000,1.060670000,1.060770000\n03/10/2017 07:16,1.06077
0000,1.060800000,1.060480000,1.060620000\n03/10/2017 07:17,1.060620000,1.060730000,1.060500000,1.06
0720000\n03/10/2017 07:18,1.060720000,1.060760000,1.060670000,1.060700000\n03/10/2017 07:19,1.06070
0000,1.060710000,1.060510000,1.060700000\n03/10/2017 07:20,1.060700000,1.060760000,1.060510000,1.06
0520000\n03/10/2017 07:21,1.060520000,1.060720000,1.060410000,1.060710000\n03/10/2017 07:22,1.06071
0000,1.060740000,1.060600000,1.060610000\n03/10/2017 07:23,1.060610000,1.060640000,1.060270000,1.06
0370000\n03/10/2017 07:24,1.060370000,1.060760000,1.060310000,1.060660000\n03/10/2017 07:25,1.06066
0000,1.060660000,1.060440000,1.060510000\n03/10/2017 07:26,1.060510000,1.060650000,1.060340000,1.06
0620000\n03/10/2017 07:27,1.060620000,1.060860000,1.060560000,1.060600000\n03/10/2017 07:28,1.06060
0000,1.060760000,1.060530000,1.060540000\n03/10/2017 07:29,1.060540000,1.061300000,1.060260000,1.06
0780000\n03/10/2017 07:30,1.060780000,1.062440000,1.060210000,1.061600000\n03/10/2017 07:31,1.06160
0000,1.062830000,1.061280000,1.062820000\n03/10/2017 07:32,1.062820000,1.063160000,1.062700000,1.06
3090000\n03/10/2017 07:33,1.063090000,1.063610000,1.063060000,1.063110000\n03/10/2017 07:34,1.06311
0000,1.063190000,1.061810000,1.061890000\n03/10/2017 07:35,1.061890000,1.062350000,1.061800000,1.06
2230000\n03/10/2017 07:36,1.062230000,1.062320000,1.061410000,1.061460000\n03/10/2017 07:37,1.06146
0000,1.062150000,1.061240000,1.061800000\n03/10/2017 07:38,1.061800000,1.062600000,1.061710000,1.06
2460000\n03/10/2017 07:39,1.062460000,1.063190000,1.061910000,1.063190000\n03/10/2017 07:40,1.06319
0000,1.063210000,1.062770000,1.063000000\n03/10/2017 07:41,1.063000000,1.063260000,1.062710000,1.06
2980000\n03/10/2017 07:42,1.062980000,1.063520000,1.062770000,1.063430000\n03/10/2017 07:43,1.06343
0000,1.063770000,1.063400000,1.063590000\n03/10/2017 07:44,1.063590000,1.063720000,1.063340000,1.06
3390000\n03/10/2017 07:45,1.063390000,1.063450000,1.063060000,1.063170000\n03/10/2017 07:46,1.06317
0000,1.064670000,1.063140000,1.064450000\n03/10/2017 07:47,1.064450000,1.064490000,1.064000000,1.06
4140000\n03/10/2017 07:48,1.064140000,1.064140000,1.063840000,1.064030000\n03/10/2017 07:49,1.06403
0000,1.064180000,1.063770000,1.063800000\n03/10/2017 07:50,1.063800000,1.063830000,1.063310000,1.06
3380000\n03/10/2017 07:51,1.063380000,1.063720000,1.063300000,1.063660000\n03/10/2017 07:52,1.06366
0000,1.063920000,1.063590000,1.063780000\n03/10/2017 07:53,1.063780000,1.063790000,1.063320000,1.06
3370000\n03/10/2017 07:54,1.063370000,1.063650000,1.063320000,1.063540000\n03/10/2017 07:55,1.06354
0000,1.063800000,1.063330000,1.063750000\n","CentralPoint":"03/10/2017 07:30","InstanceName":"US Ch
ange in NonFarm Payrolls","PeriodStart":"03/10/2017 07:06","PeriodEnd":"03/10/2017 07:55","ReportDa
te":"04/07/2017 06:30"}

输出

import pymysql
db = pymysql.connect(host='localhost',user='root',passwd='admin',port=3306)
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print ("Database version : %s " % data)
db.close()