[PYTHON] Add query to url parsed url and return to original url

Overview

Assuming that there is a URL like this, https://api.twitter.com/1.1/statuses/user_timeline.json&screen_name=drwtsn64 After ʻurllib.parse.urlparse () How to addcount = 200` to the query part and return to the original URL.

verification code
import urllib.parse

url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=drwtsn64'
parsed = urllib.parse.urlparse(url)

print(parsed)

query = urllib.parse.parse_qs(parsed.query, True)

print(query)

query['count'] = 200

print(query)

encoded = urllib.parse.urlencode(query, True)

print(encoded)

reversed = urllib.parse.ParseResult(parsed.scheme, parsed.netloc, parsed.path, parsed.params, encoded, parsed.fragment).geturl()

print(reversed)
output
ParseResult(scheme='https', netloc='api.twitter.com', path='/1.1/statuses/user_timeline.json', params='', query='screen_name=drwtsn64', fragment='')
{'screen_name': ['drwtsn64']}
{'screen_name': ['drwtsn64'], 'count': 200}
screen_name=drwtsn64&count=200
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=drwtsn64&count=200
important point

Since the query passed to ʻurlencode ()` is a dictionary, the order of the query characters is not guaranteed.

Recommended Posts

Add query to url parsed url and return to original url
Python URL and query parameter parsing summary
It's faster to add than to join and extend the list, right?
Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list
Add query to url parsed url and return to original url
Add totals to rows and columns in pandas
Add totals to rows and columns in pandas
[Python] [Django] How to use ChoiceField and how to add options
Return one-hot encoded features to the original category value