0

How do I get data from a URL?

URL:

http:\\localhost\?id=1&q=W&random_id=12002H#@&&up=down

Then I want to store it in a dictionary:

data = {
    "id": "1",
    "q": "W",
    "random_id": "12022H#@&",
    "up": "down"
}
1
  • Your URL should be encoded to work with standard html ? http:\\localhost\?id=1&q=W&random_id=12002H%23%40%26&up=down Commented May 20, 2020 at 4:25

1 Answer 1

1

As mentioned in my comment, the given URL does not look valid. I've used the valid encoded one : http:\\localhost\?id=1&q=W&random_id=12002H%23%40%26&up=down. Then you can parse it using urllib:

from urllib import parse
url = 'http:\\localhost\?id=1&q=W&random_id=12002H%23%40%26&up=down'

query = parse.urlsplit(url).query
print(query)
print(parse.parse_qsl(query))
data = dict(parse.parse_qsl(query))
print(data)

Output:

id=1&q=W&random_id=12002H%23%40%26&up=down
[('id', '1'), ('q', 'W'), ('random_id', '12002H#@&'), ('up', 'down')]
{'id': '1', 'q': 'W', 'random_id': '12002H#@&', 'up': 'down'}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.