Python--URL操作

目标URL:http://127.0.0.1:5000/oauth/authorize?redirect_uri=http%3A%2F%2F127.0.0.1%3A5000%2Fcallback%2F%3FskillId%3D18936%26token%3DNzkzODU4MzM3QUZFSElORkRWUQ%3D%3D&client_id=jwETJvFj1nkRYtVJrNZE7lS9&response_type=code&state=0.1354064097905866

URL中的元素:

  • 通常意义上的URL
  • 重定义URL:
  • 通常意思上的URL的请求参数
  • 重定义URL里面的请求参数

解析URL—urlparse

我使用的Python3.6,使用的是自带的库urllib,并使用里面的子模块parse。

from urllib.parse import urlparse

url = "https://127.0.0.1/oauth/authorize" \
      "?redirect_uri=https%3A%2F%2Fopen.bot.tmall.com%2Foauth%2Fcallback%3FskillId%3D18936%26token%3DNzkzODU4MzM3QUZFSElORkRWUQ%3D%3D" \
      "&client_id=o92cvMIa5FzGbq4LpHJoaf2j" \
      "&response_type=code" \
      "&state=0.1354064097905866"

dest_str = urlparse(url)
print(dest_str)

打印出来:

ParseResult(scheme='https', netloc='127.0.0.1', path='/oauth/authorize', params='', query='redirect_uri=https%3A%2F%2Fopen.bot.tmall.com%2Foauth%2Fcallback%3FskillId%3D18936%26token%3DNzkzODU4MzM3QUZFSElORkRWUQ%3D%3D&client_id=o92cvMIa5FzGbq4LpHJoaf2j&response_type=code&state=0.1354064097905866', fragment='')

它会解析为:

  • scheme:https
  • netloc:127.0.0.1
  • path:/oauth/authorize
  • params:
  • query:redirect_uri=https%3A%2F%2Fopen.bot.tmall.com%2Foauth%2Fcallback%3FskillId%3D18936%26token%3DNzkzODU4MzM3QUZFSElORkRWUQ%3D%3D&client_id=o92cvMIa5FzGbq4LpHJoaf2j&response_type=code&state=0.1354064097905866
  • fragment:

解析query—使用parse_qs

from urllib.parse import urlparse, parse_qs

url = "https://127.0.0.1/oauth/authorize" \
      "?redirect_uri=https%3A%2F%2Fopen.bot.tmall.com%2Foauth%2Fcallback%3FskillId%3D18936%26token%3DNzkzODU4MzM3QUZFSElORkRWUQ%3D%3D" \
      "&client_id=o92cvMIa5FzGbq4LpHJoaf2j" \
      "&response_type=code" \
      "&state=0.1354064097905866"

dest_str = urlparse(url)
print(parse_qs(dest_str.query))

打印出来:

{'redirect_uri': ['https://open.bot.tmall.com/oauth/callback?skillId=18936&token=NzkzODU4MzM3QUZFSElORkRWUQ=='], 'client_id': ['o92cvMIa5FzGbq4LpHJoaf2j'], 'response_type': ['code'], 'state': ['0.1354064097905866']}

解析redirect_uri—使用urlparse

from urllib.parse import urlparse, parse_qs

url = "https://127.0.0.1/oauth/authorize" \
      "?redirect_uri=https%3A%2F%2Fopen.bot.tmall.com%2Foauth%2Fcallback%3FskillId%3D18936%26token%3DNzkzODU4MzM3QUZFSElORkRWUQ%3D%3D" \
      "&client_id=o92cvMIa5FzGbq4LpHJoaf2j" \
      "&response_type=code" \
      "&state=0.1354064097905866"

dest_1 = urlparse(url)
dest_2 = parse_qs(dst_1.query)
dest_3 = urlparse(dest_2)
print(parse_qs(dest_str.query))

最终的

# encoding: utf-8

from urllib.parse import urlparse, parse_qs
import html

url = "https://127.0.0.1/oauth/authorize" \
      "?redirect_uri=https%3A%2F%2Fopen.bot.tmall.com%2Foauth%2Fcallback%3FskillId%3D18936%26token%3DNzkzODU4MzM3QUZFSElORkRWUQ%3D%3D" \
      "&client_id=o92cvMIa5FzGbq4LpHJoaf2j" \
      "&response_type=code" \
      "&state=0.1354064097905866"


def main():
    dest_1 = urlparse(url)
    #print(dest_1)             # ParseResult(scheme='https', netloc='127.0.0.1', path='/oauth/authorize', params='', query='redirect_uri=https%3A%2F%2Fopen.bot.tmall.com%2Foauth%2Fcallback%3FskillId%3D18936%26token%3DNzkzODU4MzM3QUZFSElORkRWUQ%3D%3D&client_id=o92cvMIa5FzGbq4LpHJoaf2j&response_type=code&state=0.1354064097905866', fragment='')

    dest_2 = parse_qs(dest_1.query)
    #print(dest_2)              # {'redirect_uri': ['https://open.bot.tmall.com/oauth/callback?skillId=18936&token=NzkzODU4MzM3QUZFSElORkRWUQ=='], 'client_id': ['o92cvMIa5FzGbq4LpHJoaf2j'], 'response_type': ['code'], 'state': ['0.1354064097905866']}

    dest_3 = dest_2['redirect_uri']
    #print(dest_3)              # ['https://open.bot.tmall.com/oauth/callback?skillId=18936&token=NzkzODU4MzM3QUZFSElORkRWUQ==']

    print(str(dest_3)[2:-2])    # https://open.bot.tmall.com/oauth/callback?skillId=18936&token=NzkzODU4MzM3QUZFSElORkRWUQ==

    dest_4 = urlparse(str(dest_3)[2:-2])
    print(dest_4)               # ParseResult(scheme='https', netloc='open.bot.tmall.com', path='/oauth/callback', params='', query='skillId=18936&token=NzkzODU4MzM3QUZFSElORkRWUQ==', fragment='')

    dest_5 = parse_qs(dest_4.query)
    print(dest_5)                     # {'skillId': ['18936'], 'token': ['NzkzODU4MzM3QUZFSElORkRWUQ==']}

    dest_6 = dest_5['token'] # ['NzkzODU4MzM3QUZFSElORkRWUQ==']
    print(str(dest_6)[2:-2]) # NzkzODU4MzM3QUZFSElORkRWUQ==


if __name__ == '__main__':
    main()

你可能感兴趣的:(Python)