{"id":20837,"date":"2023-05-28T21:54:22","date_gmt":"2023-05-28T12:54:22","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=20837"},"modified":"2023-06-25T08:06:21","modified_gmt":"2023-06-24T23:06:21","slug":"%e3%80%90python%e3%80%91%e3%80%8crequests%e3%80%8d%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=20837","title":{"rendered":"\u3010Python\u3011\u300cRequests\u300d\u306e\u4f7f\u3044\u65b9"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n\n\n\n<p>\u3082\u3057\u3001Anaconda\u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install requests<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u30a4\u30f3\u30dd\u30fc\u30c8<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">import requests<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9f\u88c5<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">response = requests.get('\u30b5\u30a4\u30c8\u306eURL')\n\nif response.status_code == 200:\n    print(response.text) # \u30ec\u30b9\u30dd\u30f3\u30b9HTML\u306e\u51fa\u529b\nelse:\n    raise # print\u306a\u3069\u3067\u30ed\u30b0\u51fa\u529b\u3067\u3082\u826f\u3044\u3067\u3059\u3002<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u30ec\u30b9\u30dd\u30f3\u30b9\u30b3\u30fc\u30c9<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">200<\/h4>\n\n\n\n<p>\u30ea\u30af\u30a8\u30b9\u30c8\u6210\u529f\u3067\u3059\u3002HTML\u304c\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">400\u301c499<\/h4>\n\n\n\n<p>\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u306e\u30a8\u30e9\u30fc\uff08\u30a2\u30af\u30bb\u30b9\u3057\u3088\u3046\u3068\u3057\u3066\u3044\u308b\u4eba\u306b\u554f\u984c\u304c\u3042\u308b\u3002\uff09<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">500~599<\/h4>\n\n\n\n<p>\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u30a8\u30e9\u30fc\uff08\u30a2\u30af\u30bb\u30b9\u5148\u306e\u30b5\u30a4\u30c8\u306b\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30d8\u30c3\u30c0\u30fc\u304c\u8981\u6c42\u3055\u308c\u308b\u30b5\u30a4\u30c8\u3082\u3042\u308b\u3002<\/h2>\n\n\n\n<p>\u4f8b\u3048\u3070\u3001Amazon\u3067\u3042\u308c\u3070\u30ea\u30af\u30a8\u30b9\u30c8\u6642\u306bheader\u306bUser-Agent\u3092\u6307\u5b9a\u3057\u306a\u3044\u3068\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d7\u3051\u4ed8\u3051\u306a\u3044\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u30b5\u30a4\u30c8\u898f\u7d04\u306b\u3088\u3063\u3066\u3053\u306e\u8fba\u306f\u307e\u3061\u307e\u3061\u3067\u3059\u3002\uff08\u30d8\u30c3\u30c0\u30fc\u304c\u306a\u304f\u3066\u3082\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d7\u3051\u4ed8\u3051\u308b\u30b5\u30a4\u30c8\u3082\u305f\u304f\u3055\u3093\u3042\u308a\u307e\u3059\u3002\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>headers = {\"User-Agent\": \"\u81ea\u5206\u81ea\u8eab\u306e\u30e6\u30fc\u30b6\u30fc\u30a8\u30fc\u30b8\u30a7\u30f3\u30c8\u540d\"}\r\nresponse = requests.get(\"https:\/\/www.amazon.co.jp\/\", headers=headers)<\/code><\/pre>\n\n\n\n<p>User-Agent\u306fChrome\u306a\u3069\u306e\u958b\u767a\u8005\u30c4\u30fc\u30eb\u306eNetwork\u30bf\u30d6\u2192Header\u306a\u3069\u3092\u30c7\u30d0\u30c3\u30b0\u3067\u304d\u308b\u306e\u3067\u4e0b\u306e\u65b9\u3092\u78ba\u8a8d\u3059\u308c\u3070\u51fa\u3066\u304f\u308b\u306e\u3067\u30b3\u30d4\u30da\u3057\u3066\u4f7f\u3046\u3068\u3088\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"538\" height=\"88\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2023\/06\/image-46.png\" alt=\"\" class=\"wp-image-21776\" srcset=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2023\/06\/image-46.png 538w, http:\/\/www.code-magagine.com\/wp-content\/uploads\/2023\/06\/image-46-300x49.png 300w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u30b5\u30a4\u30c8\u306eURL\u3092\u4e00\u62ec\u53d6\u5f97\u3057\u3066\u30ea\u30af\u30a8\u30b9\u30c8\u3059\u308b\u30c6\u30af\u30cb\u30c3\u30af<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5909\u6570\u3092\u57cb\u3081\u8fbc\u3080\u30c6\u30af\u30cb\u30c3\u30af<\/h3>\n\n\n\n<p>format\u3092\u4f7f\u3044\u307e\u3059\u3002\u5b9f\u969b\u306e\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306ffor\u30eb\u30fc\u30d7\u3092\u4f7f\u3063\u3066\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3\u304c\u3042\u308b\u8907\u6570\u306e\u30da\u30fc\u30b8\u306b\u5bfe\u3057\u3066\u9023\u7d9a\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3044\u304f\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">url = 'https:\/\/xxx\/?page={}'\ntarget_url = url.format(1)\nprint(target_url) # https:\/\/xxx\/?page=1<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8907\u6570\u30b5\u30a4\u30c8\u3092\u53d6\u5f97<\/h3>\n\n\n\n<p>\u4f8b\u3048\u3070\u3001\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3\u304c\u3042\u308b\u3088\u3046\u306a\u30b5\u30a4\u30c8\u30671\u301c3\u30da\u30fc\u30b8\u76ee\u307e\u3067\u53d6\u5f97\u3059\u308b\u3088\u3046\u306a\u30b5\u30f3\u30d7\u30eb\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from time import sleep\n\nfrom bs4 import BeautifulSoup\nimport requests\n\nurl = 'https:\/\/xxx\/?page={}' \n\n# 1\u301c3\u3092\u30eb\u30fc\u30d7\u3059\u308b\nfor i in range(1, 4):\n    # \u5909\u6570target_url\u306b\u3001\u30a2\u30af\u30bb\u30b9\u5148\u306eURL\u3092\u683c\u7d0d\u3059\u308b\n    target_url = url.format(i)\n\n    # print()\u3057\u3066target_url\u3092\u78ba\u8a8d\u3059\u308b\n    print(target_url) # https:\/\/xxx\/?page=1\n\n    r = requests.get(target_url)\n    sleep(1) # 1\u79d2\u9593\u5f85\u6a5f\n\u3000\u3000\u3000\u3000soup = BeautifulSoup(r.text)\n\n    # \u4ee5\u5f8cBeautifulSoup\u3067\u306eHTML\u89e3\u6790\u51e6\u7406\u3092\u884c\u3046\u3002\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">sleep<\/h3>\n\n\n\n<p>1URL\u3092\u30ea\u30af\u30a8\u30b9\u30c8\u3059\u308b\u305f\u3073\u306b\u5fc5\u305a1\u79d2\u9593\u306f\u9593\u9694\u3092\u958b\u3051\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002\uff08\u76f8\u624b\u306e\u30b5\u30fc\u30d0\u30fc\u3078\u306e\u8ca0\u8377\u3092\u4f4e\u6e1b\u3059\u308b\u305f\u3081\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">BeautifulSoup\u3067\u306eHTML\u89e3\u6790<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u8a18\u4e8b\u3067\u8a73\u3057\u304f\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p><blockquote class=\"wp-embedded-content\" data-secret=\"ZeooL72cpN\"><a href=\"http:\/\/www.code-magagine.com\/?p=20845\">\u3010Python\u3011\u300cBeautifulSoup\u300d\u306b\u3064\u3044\u3066<\/a><\/blockquote><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;\u3010Python\u3011\u300cBeautifulSoup\u300d\u306b\u3064\u3044\u3066&#8221; &#8212; \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30de\u30ac\u30b8\u30f3\" src=\"http:\/\/www.code-magagine.com\/?p=20845&#038;embed=true#?secret=Lj3D86ct5F#?secret=ZeooL72cpN\" data-secret=\"ZeooL72cpN\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb \u3082\u3057\u3001Anaconda\u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002 pip install requests \u30a4\u30f3\u30dd\u30fc\u30c8 import requests \u5b9f\u88c5 response =  [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[47],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/20837"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20837"}],"version-history":[{"count":11,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/20837\/revisions"}],"predecessor-version":[{"id":21777,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/20837\/revisions\/21777"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20837"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}