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 add
count = 200` to the query part and return to the original URL.
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)
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
Since the query
passed to ʻurlencode ()` is a dictionary, the order of the query characters is not guaranteed.
Recommended Posts