向URL添加查询参数

2024-05-13 10:50:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从网站自动下载数据。我需要传递动态参数的网站,每天变化。html是在表而不是表单中构造的。如何传递参数并从url获得结果?你知道吗

这就是我所尝试的,它需要在python2.7中实现

import urllib

url = "https://disc.gsfc.nasa.gov/SSW/#keywords="

params = urllib.urlencode({'keyword':"(GPM_3IMERGHHE)", 't1':"2019-01-02", 't2':"2019-01-03", 'bboxBbox':"3.52,32.34,16.88,42.89"})
r = urllib.urlopen(url, params)

return = r.read()

Tags: 数据httpsimporturl表单参数网站html
2条回答

我能够在Python2.7中实现这一点,如下所示,但是在IronPython2.7.7中,由于软件依赖性,我需要实现这一点。我得到一个错误,说“未知的url类型:https”我知道IronPython2.7.9之前的版本有https的问题。有没有办法绕过安全检查?你知道吗

>>> import urllib
>>> url_keys = urllib.urlencode( {'action': "SUBSET", 'no_attr_prefix': 1, 'content_key_is_value': 1, 'force_array': 1,
...         'pretty': 0, 'start': "2019-01-02T00:00:00Z", 'end': "2019-01-04T23:59:59Z", 'south': 0.28,
...         'west': 32.77, 'north': 13.64, 'east': 44.72, 'variables': "precipitationCal", 'format': "netCDF",
...         'dataset_id': "GPM Level 3 IMERG Early Half Hourly 0.1 x 0.1 degree Precipitation V05", 'agent_id': "OPeNDAP"})
>>> url = "https://disc.gsfc.nasa.gov/daac-bin/SSW/SSW"
>>> r = urllib.urlopen(url, url_keys)

您需要将查询参数附加到基本url,以便urllib.urlopen创建GET请求。你知道吗

>>> url = "https://disc.gsfc.nasa.gov/SSW/#keywords="

>>> params = {'keyword':"(GPM_3IMERGHHE)", 't1':"2019-01-02", 't2':"2019-01-03", 'bboxBbox':"3.52,32.34,16.88,42.89"}
>>> quoted_params = urllib.urlencode(params)
>>> quoted_params
'bboxBbox=3.52%2C32.34%2C16.88%2C42.89&t2=2019-01-03&keyword=%28GPM_3IMERGHHE%29&t1=2019-01-02'

>>> full_url = url + quoted_params
>>> full_url
'https://disc.gsfc.nasa.gov/SSW/#keywords=bboxBbox=3.52%2C32.34%2C16.88%2C42.89&t2=2019-01-03&keyword=%28GPM_3IMERGHHE%29&t1=2019-01-02'

>>> resp = urllib.urlopen(full_url)
>>> html = resp.read()

相关问题 更多 >