{"id":386,"date":"2025-02-07T12:44:50","date_gmt":"2025-02-07T13:44:50","guid":{"rendered":"https:\/\/datanalyser.ir\/?p=386"},"modified":"2025-04-19T07:51:57","modified_gmt":"2025-04-19T07:51:57","slug":"%d8%b1%d8%a7%d9%87%d9%86%d9%85%d8%a7%db%8c-%d8%a7%d8%b3%d8%aa%d9%81%d8%a7%d8%af%d9%87-%d8%a7%d8%b2-requests-%d8%af%d8%b1-%d9%be%d8%a7%db%8c%d8%aa%d9%88%d9%86","status":"publish","type":"post","link":"https:\/\/datanalyser.ir\/?p=386","title":{"rendered":"\u0631\u0627\u0647\u0646\u0645\u0627\u06cc \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 requests \u062f\u0631 \u067e\u0627\u06cc\u062a\u0648\u0646"},"content":{"rendered":"\n<p>\u062f\u0631 \u0627\u06cc\u0646 \u0631\u0627\u0647\u0646\u0645\u0627\u060c \u0646\u062d\u0648\u0647 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 \u06a9\u062a\u0627\u0628\u062e\u0627\u0646\u0647 <code>requests<\/code> \u062f\u0631 \u067e\u0627\u06cc\u062a\u0648\u0646 \u0628\u0631\u0627\u06cc \u0627\u0631\u0633\u0627\u0644 \u062f\u0631\u062e\u0648\u0627\u0633\u062a\u200c\u0647\u0627\u06cc <strong>GET<\/strong> \u0648 <strong>POST<\/strong> \u0631\u0627 \u0628\u0627 \u0645\u062b\u0627\u0644\u200c\u0647\u0627\u06cc \u0639\u0645\u0644\u06cc \u062a\u0648\u0636\u06cc\u062d \u0645\u06cc\u200c\u062f\u0647\u06cc\u0645.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u06f1. \u0646\u0635\u0628 \u06a9\u062a\u0627\u0628\u062e\u0627\u0646\u0647 requests<\/strong><\/h2>\n\n\n\n<p>\u0627\u06af\u0631 <code>requests<\/code> \u0631\u0648\u06cc \u0633\u06cc\u0633\u062a\u0645 \u0634\u0645\u0627 \u0646\u0635\u0628 \u0646\u06cc\u0633\u062a\u060c \u0645\u06cc\u200c\u062a\u0648\u0627\u0646\u06cc\u062f \u0628\u0627 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 <code>pip<\/code> \u0622\u0646 \u0631\u0627 \u0646\u0635\u0628 \u06a9\u0646\u06cc\u062f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">pip install requests\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u06f2. \u0627\u0631\u0633\u0627\u0644 \u062f\u0631\u062e\u0648\u0627\u0633\u062a GET<\/strong><\/h2>\n\n\n\n<p>\u062f\u0631\u062e\u0648\u0627\u0633\u062a <strong>GET<\/strong> \u0628\u0631\u0627\u06cc \u062f\u0631\u06cc\u0627\u0641\u062a \u062f\u0627\u062f\u0647 \u0627\u0632 \u06cc\u06a9 \u0633\u0631\u0648\u0631 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0645\u06cc\u200c\u0634\u0648\u062f. \u0645\u062b\u0627\u0644 \u0632\u06cc\u0631 \u0645\u062d\u062a\u0648\u0627\u06cc \u06cc\u06a9 API \u0639\u0645\u0648\u0645\u06cc \u0631\u0627 \u062f\u0631\u06cc\u0627\u0641\u062a \u0645\u06cc\u200c\u06a9\u0646\u062f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">import requests\n\nurl = \"https:\/\/jsonplaceholder.typicode.com\/posts\/1\"\nresponse = requests.get(url)\n\nif response.status_code == 200:\n    print(\"\u062f\u0627\u062f\u0647 \u062f\u0631\u06cc\u0627\u0641\u062a \u0634\u062f:\", response.json())\nelse:\n    print(\"\u062e\u0637\u0627 \u062f\u0631 \u062f\u0631\u06cc\u0627\u0641\u062a \u062f\u0627\u062f\u0647:\", response.status_code)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u062a\u0648\u0636\u06cc\u062d\u0627\u062a \u06a9\u062f:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>requests.get(url)<\/code>: \u06cc\u06a9 \u062f\u0631\u062e\u0648\u0627\u0633\u062a <strong>GET<\/strong> \u0628\u0647 URL \u0645\u0634\u062e\u0635\u200c\u0634\u062f\u0647 \u0627\u0631\u0633\u0627\u0644 \u0645\u06cc\u200c\u06a9\u0646\u062f.<\/li>\n\n\n\n<li><code>response.status_code<\/code>: \u0648\u0636\u0639\u06cc\u062a \u067e\u0627\u0633\u062e \u0631\u0627 \u0628\u0631\u0645\u06cc\u200c\u06af\u0631\u062f\u0627\u0646\u062f (200 \u06cc\u0639\u0646\u06cc \u0645\u0648\u0641\u0642\u06cc\u062a\u200c\u0622\u0645\u06cc\u0632).<\/li>\n\n\n\n<li><code>response.json()<\/code>: \u067e\u0627\u0633\u062e JSON \u0631\u0627 \u0628\u0647 \u06cc\u06a9 \u062f\u06cc\u06a9\u0634\u0646\u0631\u06cc \u067e\u0627\u06cc\u062a\u0648\u0646\u06cc \u062a\u0628\u062f\u06cc\u0644 \u0645\u06cc\u200c\u06a9\u0646\u062f.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u06f3. \u0627\u0631\u0633\u0627\u0644 \u062f\u0631\u062e\u0648\u0627\u0633\u062a GET \u0628\u0627 \u067e\u0627\u0631\u0627\u0645\u062a\u0631<\/strong><\/h2>\n\n\n\n<p>\u0628\u0631\u0627\u06cc \u0627\u0631\u0633\u0627\u0644 \u067e\u0627\u0631\u0627\u0645\u062a\u0631 \u062f\u0631 \u06cc\u06a9 \u062f\u0631\u062e\u0648\u0627\u0633\u062a <strong>GET<\/strong> \u0645\u06cc\u200c\u062a\u0648\u0627\u0646 \u0627\u0632 \u062f\u06cc\u06a9\u0634\u0646\u0631\u06cc <code>params<\/code> \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u06a9\u0631\u062f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">url = \"https:\/\/jsonplaceholder.typicode.com\/posts\"\nparams = {\"userId\": 1}\n\nresponse = requests.get(url, params=params)\n\nif response.status_code == 200:\n    print(\"\u062f\u0627\u062f\u0647\u200c\u0647\u0627\u06cc \u0641\u06cc\u0644\u062a\u0631\u0634\u062f\u0647:\", response.json())\nelse:\n    print(\"\u062e\u0637\u0627 \u062f\u0631 \u062f\u0631\u06cc\u0627\u0641\u062a \u062f\u0627\u062f\u0647:\", response.status_code)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u062a\u0648\u0636\u06cc\u062d\u0627\u062a:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0645\u0642\u062f\u0627\u0631 <code>params<\/code> \u0628\u0647 \u0627\u0646\u062a\u0647\u0627\u06cc URL \u0627\u0636\u0627\u0641\u0647 \u0645\u06cc\u200c\u0634\u0648\u062f (<code>?userId=1<\/code>).<\/li>\n\n\n\n<li>\u062f\u0627\u062f\u0647\u200c\u0647\u0627\u06cc \u0628\u0627\u0632\u06af\u0634\u062a\u06cc \u0645\u0631\u0628\u0648\u0637 \u0628\u0647 <code>userId<\/code> \u0645\u0634\u062e\u0635\u200c\u0634\u062f\u0647 \u062e\u0648\u0627\u0647\u062f \u0628\u0648\u062f.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u06f4. \u0627\u0631\u0633\u0627\u0644 \u062f\u0631\u062e\u0648\u0627\u0633\u062a POST<\/strong><\/h2>\n\n\n\n<p>\u062f\u0631\u062e\u0648\u0627\u0633\u062a <strong>POST<\/strong> \u0628\u0631\u0627\u06cc \u0627\u0631\u0633\u0627\u0644 \u062f\u0627\u062f\u0647 \u0628\u0647 \u0633\u0631\u0648\u0631 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0645\u06cc\u200c\u0634\u0648\u062f. \u0645\u062b\u0627\u0644 \u0632\u06cc\u0631 \u06cc\u06a9 \u067e\u0633\u062a \u062c\u062f\u06cc\u062f \u0628\u0647 \u06cc\u06a9 API \u0627\u0631\u0633\u0627\u0644 \u0645\u06cc\u200c\u06a9\u0646\u062f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">url = \"https:\/\/jsonplaceholder.typicode.com\/posts\"\ndata = {\n    \"title\": \"\u0645\u062b\u0627\u0644 \u067e\u0627\u06cc\u062a\u0648\u0646\",\n    \"body\": \"\u0627\u06cc\u0646 \u06cc\u06a9 \u0645\u062a\u0646 \u0622\u0632\u0645\u0627\u06cc\u0634\u06cc \u0627\u0633\u062a.\",\n    \"userId\": 1\n}\n\nresponse = requests.post(url, json=data)\n\nif response.status_code == 201:  # 201: Created\n    print(\"\u067e\u0627\u0633\u062e \u0633\u0631\u0648\u0631:\", response.json())\nelse:\n    print(\"\u062e\u0637\u0627 \u062f\u0631 \u0627\u0631\u0633\u0627\u0644 \u062f\u0627\u062f\u0647:\", response.status_code)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u062a\u0648\u0636\u06cc\u062d\u0627\u062a:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>requests.post(url, json=data)<\/code>: \u062f\u0627\u062f\u0647\u200c\u0647\u0627 \u0631\u0627 \u0628\u0647 \u0639\u0646\u0648\u0627\u0646 JSON \u0627\u0631\u0633\u0627\u0644 \u0645\u06cc\u200c\u06a9\u0646\u062f.<\/li>\n\n\n\n<li><code>201 Created<\/code>: \u0646\u0634\u0627\u0646\u200c\u062f\u0647\u0646\u062f\u0647 \u0645\u0648\u0641\u0642\u06cc\u062a\u200c\u0622\u0645\u06cc\u0632 \u0628\u0648\u062f\u0646 \u0627\u06cc\u062c\u0627\u062f \u06cc\u06a9 \u0645\u0646\u0628\u0639 \u062c\u062f\u06cc\u062f \u0627\u0633\u062a.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u06f5. \u0627\u0631\u0633\u0627\u0644 \u062f\u0631\u062e\u0648\u0627\u0633\u062a POST \u0628\u0627 \u0647\u062f\u0631<\/strong><\/h2>\n\n\n\n<p>\u06af\u0627\u0647\u06cc \u0646\u06cc\u0627\u0632 \u0627\u0633\u062a \u06a9\u0647 \u062f\u0627\u062f\u0647\u200c\u0647\u0627 \u0631\u0627 \u0647\u0645\u0631\u0627\u0647 \u0628\u0627 <strong>Headers<\/strong> \u0627\u0631\u0633\u0627\u0644 \u06a9\u0646\u06cc\u0645:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">headers = {\n    \"Content-Type\": \"application\/json\",\n    \"Authorization\": \"Bearer your_token_here\"\n}\n\nresponse = requests.post(url, json=data, headers=headers)\n\nprint(\"\u067e\u0627\u0633\u062e \u0633\u0631\u0648\u0631:\", response.json())\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u062a\u0648\u0636\u06cc\u062d\u0627\u062a:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Content-Type: application\/json<\/code>: \u0645\u0634\u062e\u0635 \u0645\u06cc\u200c\u06a9\u0646\u062f \u06a9\u0647 \u062f\u0627\u062f\u0647\u200c\u0647\u0627 \u062f\u0631 \u0642\u0627\u0644\u0628 JSON \u0647\u0633\u062a\u0646\u062f.<\/li>\n\n\n\n<li><code>Authorization<\/code>: \u0627\u06af\u0631 \u0633\u0631\u0648\u0631 \u0646\u06cc\u0627\u0632 \u0628\u0647 \u0627\u062d\u0631\u0627\u0632 \u0647\u0648\u06cc\u062a \u062f\u0627\u0634\u062a\u0647 \u0628\u0627\u0634\u062f\u060c \u0628\u0627\u06cc\u062f \u062a\u0648\u06a9\u0646 \u0645\u0639\u062a\u0628\u0631 \u0627\u0631\u0633\u0627\u0644 \u0634\u0648\u062f.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u06f6. \u0645\u062f\u06cc\u0631\u06cc\u062a \u062e\u0637\u0627\u0647\u0627 \u0648 \u0627\u0633\u062a\u062b\u0646\u0627\u0647\u0627<\/strong><\/h2>\n\n\n\n<p>\u0628\u0631\u0627\u06cc \u0645\u062f\u06cc\u0631\u06cc\u062a \u062e\u0637\u0627\u0647\u0627\u06cc \u0634\u0628\u06a9\u0647 \u0648 \u0633\u0631\u0648\u0631\u060c \u0645\u06cc\u200c\u062a\u0648\u0627\u0646 \u0627\u0632 <strong>try-except<\/strong> \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u06a9\u0631\u062f:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">try:\n    response = requests.get(\"https:\/\/jsonplaceholder.typicode.com\/posts\/invalid-url\")\n    response.raise_for_status()  # \u062e\u0637\u0627\u0647\u0627\u06cc HTTP \u0645\u0627\u0646\u0646\u062f 404 \u0631\u0627 \u0645\u062f\u06cc\u0631\u06cc\u062a \u0645\u06cc\u200c\u06a9\u0646\u062f\n    print(response.json())\nexcept requests.exceptions.HTTPError as errh:\n    print(\"\u062e\u0637\u0627\u06cc HTTP:\", errh)\nexcept requests.exceptions.ConnectionError as errc:\n    print(\"\u062e\u0637\u0627\u06cc \u0627\u062a\u0635\u0627\u0644:\", errc)\nexcept requests.exceptions.Timeout as errt:\n    print(\"\u062e\u0637\u0627\u06cc \u062a\u0627\u06cc\u0645\u200c\u0627\u0648\u062a:\", errt)\nexcept requests.exceptions.RequestException as err:\n    print(\"\u062e\u0637\u0627\u06cc \u0639\u0645\u0648\u0645\u06cc \u062f\u0631\u062e\u0648\u0627\u0633\u062a:\", err)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u062c\u0645\u0639\u200c\u0628\u0646\u062f\u06cc:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>raise_for_status()<\/code>: \u062e\u0637\u0627\u0647\u0627\u06cc HTTP \u0631\u0627 \u0628\u0631\u0631\u0633\u06cc \u0645\u06cc\u200c\u06a9\u0646\u062f.<\/li>\n\n\n\n<li><code>requests.exceptions.ConnectionError<\/code>: \u062f\u0631 \u0635\u0648\u0631\u062a \u0639\u062f\u0645 \u0627\u062a\u0635\u0627\u0644 \u0628\u0647 \u0627\u06cc\u0646\u062a\u0631\u0646\u062a \u06cc\u0627 \u0633\u0631\u0648\u0631.<\/li>\n\n\n\n<li><code>requests.exceptions.Timeout<\/code>: \u0627\u06af\u0631 \u062f\u0631\u062e\u0648\u0627\u0633\u062a \u0628\u06cc\u0634 \u0627\u0632 \u062d\u062f \u0637\u0648\u0644 \u0628\u06a9\u0634\u062f.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u062f\u0631 \u0627\u06cc\u0646 \u0631\u0627\u0647\u0646\u0645\u0627\u060c \u0646\u062d\u0648\u0647 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 \u06a9\u062a\u0627\u0628\u062e\u0627\u0646\u0647 requests \u062f\u0631 \u067e\u0627\u06cc\u062a\u0648\u0646 \u0628\u0631\u0627\u06cc \u0627\u0631\u0633\u0627\u0644 \u062f\u0631\u062e\u0648\u0627\u0633\u062a\u200c\u0647\u0627\u06cc GET \u0648 POST \u0631\u0627 \u0628\u0627 \u0645\u062b\u0627\u0644\u200c\u0647\u0627\u06cc \u0639\u0645\u0644\u06cc \u062a\u0648\u0636\u06cc\u062d \u0645\u06cc\u200c\u062f\u0647\u06cc\u0645.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,44],"tags":[94,122],"class_list":["post-386","post","type-post","status-publish","format-standard","hentry","category-43","category-44","tag-94","tag-122"],"_links":{"self":[{"href":"https:\/\/datanalyser.ir\/index.php?rest_route=\/wp\/v2\/posts\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datanalyser.ir\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datanalyser.ir\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datanalyser.ir\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datanalyser.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=386"}],"version-history":[{"count":2,"href":"https:\/\/datanalyser.ir\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":388,"href":"https:\/\/datanalyser.ir\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions\/388"}],"wp:attachment":[{"href":"https:\/\/datanalyser.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datanalyser.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datanalyser.ir\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}