Python-Requests,从字符串中提取url参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28328890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:06:42  来源:igfitidea点击:

Python-Requests, extract url parameters from a string

pythonpython-3.xpython-requests

提问by Gab

I am using this awesome library called requeststo maintain python 2 & 3 compatibility and simplify my application requests management.

我正在使用这个很棒的库requests来维护 python 2 & 3 兼容性并简化我的应用程序请求管理。

I have a case where I need to parse a url and replace one of it's parameter. E.g:

我有一个案例,我需要解析一个 url 并替换它的一个参数。例如:

http://example.com?param1=a&token=TOKEN_TO_REPLACE&param2=c

And I want to get this:

我想得到这个:

http://example.com?param1=a&token=NEW_TOKEN&param2=c

With the urllibI can achieve it this way:

有了urllib我可以通过这种方式实现它:

from urllib.parse import urlparse
from urllib.parse import parse_qs
from urllib.parse import urlencode

url = 'http://example.com?param1=a&token=TOKEN_TO_REPLACE&param2=c'

o = urlparse(url)
query = parse_qs(o.query)
if query.get('token'):
    query['token'] = ['NEW_TOKEN', ]
    new_query = urlencode(query, doseq=True)
    url.split('?')[0] + '?' + new_query

>>>?http://example.com?param2=c&param1=a&token=NEW_TOKEN

How can you achieve the same using the requestslibrary?

您如何使用requests库实现相同的目标?

采纳答案by Martijn Pieters

You cannot use requestsfor this; the library buildssuch URLs if passed a Python structure for the parameters, but does not offer any tools to parse them. That's not a goal of the project.

您不能requests为此使用;如果传递参数的 Python 结构,库会构建此类 URL,但不提供任何工具来解析它们。这不是该项目的目标。

Stick to the urllib.parsemethod to parse out the parameters. Once you have a dictionary or list of key-value tuples, just pass that to requeststo build the URL again:

坚持urllib.parse方法解析出参数。有了字典或键值元组列表后,只需将其传递requests给以再次构建 URL:

try:
    # Python 3
    from urllib.parse import urlparse, parse_qs
except ImportError:
    # Python 2
    from urlparse import urlparse, parse_qs

o = urlparse(url)
query = parse_qs(o.query)
# extract the URL without query parameters
url = o._replace(query=None).geturl()

if 'token' in query:
    query['token'] = 'NEW_TOKEN'

requests.get(url, params=query)

You can get both the urlparseand parse_qsfunctions in both Python 2 and 3, all you need to do is adjust the import location if you get an exception.

您可以在 Python 2 和 3 中同时获得urlparseparse_qs函数,如果遇到异常,您需要做的就是调整导入位置。

Demo on Python 3 (without the import exception guard) to demonstrate the URL having been built:

在 Python 3 上的演示(没有导入异常保护)来演示已构建的 URL:

>>> from urllib.parse import urlparse, parse_qs
>>> url = "http://httpbin.org/get?token=TOKEN_TO_REPLACE&param2=c"
>>> o = urlparse(url)
>>> query = parse_qs(o.query)
>>> url = o._replace(query=None).geturl()
>>> if 'token' in query:
...     query['token'] = 'NEW_TOKEN'
... 
>>> response = requests.get(url, params=query)
>>> print(response.text)
{
  "args": {
    "param2": "c", 
    "token": "NEW_TOKEN"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.5.1 CPython/3.4.2 Darwin/14.1.0"
  }, 
  "origin": "188.29.165.245", 
  "url": "http://httpbin.org/get?token=NEW_TOKEN&param2=c"
}

回答by Dorik1972

Using requestsonly:

requests仅使用:

query = requests.utils.urlparse(url).query
params = dict(x.split('=') for x in query.split('&'))

if 'token' in params:
    params['token'] = 'NEW_TOKEN'

requests.get(url, params=params)