Как urlencode строки запроса в Python?
Вам нужно передать ваши параметры в urlencode() как отображение (dict) или последовательность из двух кортежей, например:
>>> import urllib >>> f = < 'eventName' : 'myEvent', 'eventDescription' : 'cool event'>>>> urllib.urlencode(f) 'eventName=myEvent&eventDescription=cool+event'
>>> urllib.parse.urlencode(f) eventName=myEvent&eventDescription=cool+event
Обратите внимание, что это не делает кодирование URL в общепринятом смысле этого слова (посмотрите на вывод). Для этого используйте urllib.parse.quote_plus .
«Обратите внимание, что urllib.urlencode не всегда справляется с задачей. Проблема в том, что некоторые службы заботятся о порядке аргументов, который теряется при создании словаря. В таких случаях urllib.quote_plus лучше, как предложил Рики. «
Для Python 3 импорт должен быть следующим: import urllib.parse и вызов функции urllib.parse.urlencode(. )
и как можно это сделать, если вы просто хотите сделать безопасный URL-адрес строки, не создавая строку аргумента полного запроса?
@ Mike’Pomax’Kamermans — см., Например, stackoverflow.com/questions/12082314/… или ответ Рики на этот вопрос.
Вы можете передать список кортежей в urlencode, и порядок будет сохранен: urllib.urlencode([(‘name’, ‘brandon’), (‘x’, 1000)]) выдает ‘name=brandon&x=1000’ (как ответ состояния, хотя он не предоставляет пример кода для этого)
@ Blairg23 Если одни и те же клавиши не повторяются несколько раз, разве это не сработает, если просто передать OrderedDict ?
@JohnStrood — Python 2.7 и более ранние urlparse имеют urlparse , urllib.parse использует urllib.parse
В таком случае почему бы просто не работать с исходной строкой запроса, а не с ее разложением синтаксическим анализатором, который доступен для легкого доступа к отдельным значениям параметров.
Python 2
>>> urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$') 'string_of_characters_like_these%3A%24%23%40%3D%3F%25%5EQ%5E%24'
Python 3
В Python 3 пакет urllib разбит на более мелкие компоненты. Вы будете использовать urllib.parse.quote_plus (обратите внимание на дочерний модуль parse )
import urllib.parse urllib.parse.quote_plus(. )
Спасибо! В моем случае, однако, мне нужно поставить: import urllib.parse . urllib.parse.quote_plus(query)
очень хорошо, но почему не используется Unicode? если строка URL-адреса Unicode, я должен кодировать его в UTF-8. Есть ли другой способ сделать это?
@cbare Подобен Like quote(), but also replaces spaces by plus signs, as required for quoting HTML form values when building up a query string to go into a URL. Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to ‘/’. по ссылке.
Это прекрасно работает, но я не мог получить доступ к некоторым онлайн-сервисам (REST), пока не добавил этот параметр safe = ‘; /?: @ & = + $,’
quote_plus () кодирует пробелы как знаки плюс, как указано @kyrias, quote () кодирует пробелы как «% 20»
Контекст
Проблема
- Вы хотите создать строку запроса urlencoded.
- У вас есть словарь или объект, содержащий пары имя-значение.
- Вы хотите иметь возможность управлять порядком вывода пар имя-значение.
Решение
Ловушки
Пример
Ниже приведено полное решение, в том числе о том, как справиться с некоторыми подводными камнями.
### ******************** ## init python (version 2.7.2 ) import urllib ### ******************** ## first setup a dictionary of name-value pairs dict_name_value_pairs = < "bravo" : "True != False", "alpha" : "http://www.example.com", "charlie" : "hello world", "delta" : "1234567 !@#$%^&*", "echo" : "[email protected]", > ### ******************** ## setup an exact ordering for the name-value pairs ary_ordered_names = [] ary_ordered_names.append('alpha') ary_ordered_names.append('bravo') ary_ordered_names.append('charlie') ary_ordered_names.append('delta') ary_ordered_names.append('echo') ### ******************** ## show the output results if('NO we DO NOT care about the ordering of name-value pairs'): queryString = urllib.urlencode(dict_name_value_pairs) print queryString """ echo=user%40example.com&bravo=True+%21%3D+False&delta=1234567+%21%40%23%24%25%5E%26%2A&charlie=hello+world&alpha=http%3A%2F%2Fwww.example.com """ if('YES we DO care about the ordering of name-value pairs'): queryString = "&".join( [ item+'='+urllib.quote_plus(dict_name_value_pairs[item]) for item in ary_ordered_names ] ) print queryString """ alpha=http%3A%2F%2Fwww.example.com&bravo=True+%21%3D+False&charlie=hello+world&delta=1234567+%21%40%23%24%25%5E%26%2A&echo=user%40example.com """
How to Send a POST Request Using urllib in Python?
In this article, you will learn how to send a POST request using the urllib library in Python.
Once you have gone through the procedure to send POST requests via urllib, we will also have a look at how to send POST requests using the requests library which is used to handle GET and POST requests in Python. So, get ready to unearth the answers to the following questions –
- How to send a POST request using urllib in Python?
- How to send a POST request using the requests library in Python?
You can use a POST request to send data to a server to create/update a resource. When you send the data to a server using a POST request, the data is stored in the request body of the HTTP request. For example, you can use a POST request to send ‘key:value’ pairs in a dictionary to a web server for storage.
Solution: Use urllib.request.urlopen() and urllib.request.Request()
Approach: Call the urlencode(data) method of the urllib.parse library to encode the data object (dictionary data) to a string. Create the request object using Request(url, urlencode(data).encode()) . Here, url denotes the url of the source/server while the second parameter is the encoded data object. Once the request object has been created, send the POST request by calling urlopen(req) where req is the request object.
The response returned by the server is a file-like object. You can read the body of this response, i.e., the HTML content contained within the response using the read() function which will return the HTML content of the response as a byte object. Use the decode() function to convert this byte object to a string. All of this can be done in a single line like so: urlopen(req).read().decode()
from urllib.parse import urlencode from urllib.request import Request, urlopen url = 'https://httpbin.org/post' # Set destination URL here data = # Set POST fields here req = Request(url, urlencode(data).encode()) response = urlopen(req).read().decode() print(response)
< "args": <>, "data": "", "files": <>, "form": < "name": "Bruce Wayne" >, "headers": < "Accept-Encoding": "identity", "Content-Length": "16", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Python-urllib/3.9", "X-Amzn-Trace-Id": "Root=1-6332ab8d-4c3577a95da062ec0172ee78" >, "json": null, "origin": "49.37.37.140", "url": "https://httpbin.org/post" >
urllib.request.Request() represents a Request object that represents the HTTP request you want to make to the source URL. Actually, the class urllib.request.Request() is nothing but an abstraction of the URL request.
You can define the urllib.request.Request class as: class urllib.request.Request(url, data=None, headers<>, origin_req_host=None, unverifiable=False, method=None)
Now that you have conquered the art of sending POST requests via urllib, let’s dive into some other Python libraries that also allow you to send POST requests.
Sending a POST Request Using the requests Library in Python
The requests library facilitates you with the post() method to send a POST request to a web server. You can simply send the dictionary data along with the request url in your post request as: requests.post(url, dict_data)
import requests url = 'https://httpbin.org/post' params = res = requests.post(url, params) print(res.text)
< "args": <>, "data": "", "files": <>, "form": < "name": "Bruce Wayne" >, "headers": < "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "16", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.27.1", "X-Amzn-Trace-Id": "Root=1-6332b10b-6b9deea17583b16f31c092f5" >, "json": null, "origin": "49.37.37.140", "url": "https://httpbin.org/post" >
Example 2: The following example demonstrates how you can send JSON data in your POST request. You can use the json module in this case and supply a json formatted string within the POST request. This can be done using json.dumps().
import requests import json url = 'https://httpbin.org/post' payload = res = requests.post(url, data=json.dumps(payload)) print(res.text)
< "args": <>, "data": "", "files": <>, "form": <>, "headers": < "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "29", "Host": "httpbin.org", "User-Agent": "python-requests/2.27.1", "X-Amzn-Trace-Id": "Root=1-6332b266-35e371d31afd6c6d59e3b192" >, "json": < "Spiderman": "Peter Parker" >, "origin": "49.37.37.140", "url": "https://httpbin.org/post" >
Exercise
Challenge: Before we wrap our discussion here’s a task for you. Consider that you have been given the following url: “http://eprint.com.hr/demo/login.php“. Use the username as ‘User1’ and the password as ‘123456’. Can you use a POST request to successfully login?
Solution: We will be using the requests library to solve this programming challenge. Define a dictionary to store the key-value pairs: ‘username’:’User1′ and ‘password’:’123456′ . You can send these values as data along with the POST request to initiate the login. Here’s the code that demonstrates the solution:
import requests url = 'http://eprint.com.hr/demo/login.php' def submit_form(): payload = resp = requests.post(url, payload) print(resp.text) submit_form()
Conclusion
With that we come to the end of this tutorial and I hope this helped you with your queries. You learned how to send POST requests using the urllib and requests library with the help of numerous examples.
Please subscribe and stay tuned for more interesting discussions and solutions. Happy coding! 🙂
One of the most sought-after skills on Fiverr and Upwork is web scraping.
Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
This course teaches you the ins and outs of Python’s BeautifulSoup library for web scraping.
I am a professional Python Blogger and Content creator. I have published numerous articles and created courses over a period of time. Presently I am working as a full-time freelancer and I have experience in domains like Python, AWS, DevOps, and Networking.
Be on the Right Side of Change 🚀
- The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. 🤖
- Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
- Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.
Learning Resources 🧑💻
⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!
Join the Finxter Academy and unlock access to premium courses 👑 to certify your skills in exponential technologies and programming.
New Finxter Tutorials:
Finxter Categories: