{"componentChunkName":"component---src-templates-material-js","path":"/workshops/2021/05/21/pishem-svoego-robota-na-python-urok-5/index.html","result":{"pageContext":{"pageParameters":{"isPreview":false,"isMirror":true,"delay_to_show_donate_popup_seconds":7,"about_page":false,"games_page":true,"games_page_link_in_header":false,"donate_popup_show_ru":true,"donate_popup_title_ru":"Без вас — нас не будет","donate_popup_content_ru":"Помогите нам добывать и рассказывать важные истории. Участие каждого — важно!","donate_popup_show_en":true,"donate_popup_title_en":"We can’t do it alone!","donate_popup_content_en":"Help us uncover and share the stories that matter. Every contribution makes a difference","display_regular_donors_banner":false,"showPatreonMethod":true,"useNewMaterialDonationBanner":true,"new_donations_page_design":true,"donations_popup_variants":{"ru":[{"id":"kc19b","title":"Сохраните журналистику для людей в России ","subtitle":"Читают нас прежде всего люди в России, но будущее «Важных историй» зависит от тех, кто живет за пределами страны. Поддержите нас ежемесячным донатом — сохраните независимую журналистику для ваших близких, которые остаются в России.","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"},{"id":"33tc9","title":"У пропаганды есть миллиарды из бюджета. У нас — только вы","subtitle":"Перемены возможны, если независимые от власти журналисты продолжают свою работу день за днем, год за годом. Мы рассказываем важные истории уже шесть лет. Помогите нам продолжать.","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"}],"en":[{"id":"nj1py","title":"Important stories don’t tell themselves","subtitle":"Independent journalism comes at a cost. We answer to no owners or advertisers — only to you, our readers.  Your ongoing support allows us to keep exposing the truth. Without you, we won’t be here.","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"},{"id":"xdfc1","title":"Help us outlast the Kremlin censorship","subtitle":"Censorship lose to journalism that keeps showing up, year after year. Monthly donors are what make that possible","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"},{"id":"ck121","title":"State propaganda has billions to spend. We have only you to rely on","subtitle":"Change happens when independent journalists keep showing up — day after day, year after year. Six years in, we're still showing up. Help us keep going with a monthly donation.","is_active":true,"background":"linear-gradient(10deg, #421f20 5%, #994146)"}]},"feedback_page":true,"donorsCount":0,"showAuthorsRequestAfterMaterial":true,"radioPage":true,"radioExists":{"ru":true,"en":false},"singlePlaylistPath":{"ru":"/radio/2025/12/22/razgovori-ovazhnikh/index.html"},"with_bookmarks":true,"news_feed":true,"news_feed_skeleton":false,"news_page_redesign":true,"with_topics":true,"materials_topics":false,"topics_page":true,"with_pdf_share":false,"with_native_share":false,"preferred_source_page":true,"charon_new_form":true,"preferred_source_promos":true,"charon_en_page":true},"pk":266,"header":"Пишем своего робота на Python. Урок 5","lead":"Улучшаем функцию, которая делает запросы к API","category":"workshops","entry_image":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-1920x960px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-1920x960px_2x.webp","for_width":1920,"for_height":960,"real_width_1x":1920,"real_width_2x":3840,"real_height_1x":960,"real_height_2x":1920},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-1920x960px_1x.png","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-1920x960px_2x.png","for_width":1920,"for_height":960,"real_width_1x":1920,"real_width_2x":3840,"real_height_1x":960,"real_height_2x":1920},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-1272x636px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-1272x636px_2x.webp","for_width":1272,"for_height":636,"real_width_1x":1272,"real_width_2x":2544,"real_height_1x":636,"real_height_2x":1272},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-1272x636px_1x.png","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-1272x636px_2x.png","for_width":1272,"for_height":636,"real_width_1x":1272,"real_width_2x":2544,"real_height_1x":636,"real_height_2x":1272},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-912x456px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-912x456px_2x.webp","for_width":912,"for_height":456,"real_width_1x":912,"real_width_2x":1824,"real_height_1x":456,"real_height_2x":912},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-912x456px_1x.png","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-912x456px_2x.png","for_width":912,"for_height":456,"real_width_1x":912,"real_width_2x":1824,"real_height_1x":456,"real_height_2x":912},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-600x400px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-600x400px_2x.webp","for_width":600,"for_height":400,"real_width_1x":600,"real_width_2x":1200,"real_height_1x":400,"real_height_2x":800},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-600x400px_1x.png","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-600x400px_2x.png","for_width":600,"for_height":400,"real_width_1x":600,"real_width_2x":1200,"real_height_1x":400,"real_height_2x":800},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-360x360px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-360x360px_2x.webp","for_width":360,"for_height":360,"real_width_1x":360,"real_width_2x":720,"real_height_1x":360,"real_height_2x":720},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-360x360px_1x.png","url_2x":"https://static.istories.media/uploaded/images/7a2584790d254c62997eca95d0bbfcd8-360x360px_2x.png","for_width":360,"for_height":360,"real_width_1x":360,"real_width_2x":720,"real_height_1x":360,"real_height_2x":720}],"entry_image_credit":"","blocks":[{"data":"Продолжаем писать робота, который будет присылать нам на почту подборку госконтрактов. Сегодня мы будем совершенствовать функцию, которая делает запрос к API сервиса «ГосЗатраты». Как всегда, начинаем с открытия проект и запуска виртуального окружения с помощью <strong>pipenv shell</strong>.&nbsp;","type":"p","className":""},{"data":{"id":2464,"data":{"url":"https://www.youtube.com/embed/yYpYzQD1bJo","text":"Видео: Глеб Лиманский","provider":"youtube","rawInput":"https://www.youtube.com/watch?v=yYpYzQD1bJo","ratio_mobile":"16:9","ratio_desktop":"16:9"},"category":"video-embed"},"type":"video-embed"},{"data":"Сейчас наша функция получения данных устроена таким образом, что принимает на вход код региона заказчика, подставляет его в запрос, и мы получаем выдачу госконтрактов. Но если мы посмотрим в <a href=\"https://github.com/clearspending/clearspending-examples/wiki\" data-type=\"usual\" target=\"_blank\">документацию к API</a>, то увидим, что опций, которые можно использовать для запроса, гораздо больше. Предугадать эти опции в нашей функции невозможно, поэтому мы будем использовать именованные аргументы, сокращенно kwargs. С их помощью мы сможем передавать любой из этих параметров в нашу функцию, по ним будет составляться запрос и формироваться наша выдача.","type":"p","className":""},{"data":"Перепишем функцию: удалим из строчки <strong>url</strong> запрос <strong>customerregion</strong>, а <strong>region_code</strong> заменим на именованные аргументы, используя двойную звездочку <strong>**</strong> и слово <strong>kwargs</strong>. Когда мы будем вызывать эту функцию, вместо <strong>kwargs</strong> будем указывать нужные аргументы, которые хотим добавить в <strong>url</strong>, .","type":"p","className":""},{"data":{"id":2394,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/282db72c44fc4ca89dae7a4f4215eb07-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/282db72c44fc4ca89dae7a4f4215eb07-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":579,"real_height_2x":1158},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/282db72c44fc4ca89dae7a4f4215eb07-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/282db72c44fc4ca89dae7a4f4215eb07-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":579,"real_height_2x":1158}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"С помощью этого синтаксиса мы можем передавать в нашу функцию переменное количество аргументов. Чтобы использовать их, введем переменную <strong>params</strong>, где распакуем наши именованные аргументы. Теперь, когда мы будем отправлять запрос к серверу «ГосЗатрат», мы должны указать в <strong>data</strong> не только <strong>url</strong>, но и <strong>params</strong>.","type":"p","className":""},{"data":{"id":2395,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/316245c73eb1471eaee7503cfd3401c2-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/316245c73eb1471eaee7503cfd3401c2-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":434,"real_height_2x":868},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/316245c73eb1471eaee7503cfd3401c2-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/316245c73eb1471eaee7503cfd3401c2-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":434,"real_height_2x":868}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"Проверим, как это работает. Например, мы хотим получить все исполненные контракты, где поставщиком выступала компания Игоря Ротенберга. Таким образом, у нас есть два параметра: <strong>supplierinn</strong> и <strong>currentstage</strong> с выбранным правильным значением.","type":"p","className":""},{"data":{"id":2393,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/03a03794954c45a288e9fe1a99a5c084-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/03a03794954c45a288e9fe1a99a5c084-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":1396,"real_height_2x":2791},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/03a03794954c45a288e9fe1a99a5c084-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/03a03794954c45a288e9fe1a99a5c084-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":1396,"real_height_2x":2791}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"Добавляем в <strong>result</strong> желаемый <strong>supplierinn</strong> и <strong>currentstage</strong>, после чего запускаем функцию —&nbsp;и получаем в терминале ровно то, что нам было нужно.&nbsp;","type":"p","className":""},{"data":{"id":2396,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/98a784ff07fa47bea028b3eae0c88a99-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/98a784ff07fa47bea028b3eae0c88a99-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":690,"real_height_2x":1381},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/98a784ff07fa47bea028b3eae0c88a99-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/98a784ff07fa47bea028b3eae0c88a99-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":690,"real_height_2x":1381}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"Теперь наша функция выглядит универсально — мы можем подавать ей на вход любые доступные параметры поиска. Появятся вопросы — не стесняйтесь задавать их в нашем <a href=\"https://t.me/istories_helloworld\" data-type=\"usual\" target=\"_blank\">чате</a> в Telegram.","type":"p","className":""}],"created_at_date":"2021-05-21","updated_at_date":null,"created_at_date_time":null,"updated_at_date_time":null,"is_exclusive":false,"is_urgent":false,"initially_published_at":"2021-05-23T10:20:38.285Z","has_additional_facts":false,"locale":"ru","has_visualization_without_fallback":false,"spec_project":null,"header_secondary":"","warning":"","is_translated_by_ai":false,"with_donation_banner":true,"donation_banner_header":"","donation_banner_text":"","media_outlet__codename":null,"with_authors_request_banner":false,"faq":[],"_path":"/workshops/2021/05/21/pishem-svoego-robota-na-python-urok-5/","is_published":true,"authors_list":[{"id":8,"lead":"Главная редакторка «Важных историй»","_path":"alesya-marokhovskaya","avatar":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-896x504px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-896x504px_2x.webp","for_width":896,"for_height":504,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":896,"real_width_2x":1792,"real_height_1x":504,"real_height_2x":1008},{"format":"jpg","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-896x504px_1x.jpg","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-896x504px_2x.jpg","for_width":896,"for_height":504,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":896,"real_width_2x":1792,"real_height_1x":504,"real_height_2x":1008},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-688x387px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-688x387px_2x.webp","for_width":688,"for_height":387,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":688,"real_width_2x":1376,"real_height_1x":387,"real_height_2x":774},{"format":"jpg","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-688x387px_1x.jpg","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-688x387px_2x.jpg","for_width":688,"for_height":387,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":688,"real_width_2x":1376,"real_height_1x":387,"real_height_2x":774},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-416x234px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-416x234px_2x.webp","for_width":416,"for_height":234,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":416,"real_width_2x":832,"real_height_1x":234,"real_height_2x":468},{"format":"jpg","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-416x234px_1x.jpg","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-416x234px_2x.jpg","for_width":416,"for_height":234,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":416,"real_width_2x":832,"real_height_1x":234,"real_height_2x":468},{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-336x189px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-336x189px_2x.webp","for_width":336,"for_height":189,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":336,"real_width_2x":672,"real_height_1x":188,"real_height_2x":378},{"format":"jpg","url_1x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-336x189px_1x.jpg","url_2x":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-336x189px_2x.jpg","for_width":336,"for_height":189,"original_url":"https://static.istories.media/uploaded/images/12285015ded6416c9ce475e3a939d7f7-original.jpg","real_width_1x":336,"real_width_2x":672,"real_height_1x":188,"real_height_2x":378}],"lead_en":"Editor-in-chief","ordering":null,"position":"Редакторка","is_public":true,"last_name":"Мароховская","first_name":"Алеся","position_en":"Editor","last_name_en":"Marokhovskaya","first_name_en":"Alesya"}],"external_authors_list":[],"editors_list":[],"external_editors_list":[],"participants_list":[],"external_participants_list":[],"partners_list":[],"case":null,"translated_material":null,"audio_material":null,"text_material":null,"id":266,"topics":[],"meta":{"og_title":"","og_description":"","twitter_title":"","twitter_description":"","og_image":"https://static.istories.media/uploaded/images/share/6f6c6fc4299743198e7d7ec634c576d6.png","twitter_image":"https://static.istories.media/uploaded/images/share/6f6c6fc4299743198e7d7ec634c576d6.png","vk_image":"https://static.istories.media/uploaded/images/share/vk/7eeed14e3f1f46edbecc8b69dee5feeb.png"},"tags":["ddj","python","api"],"playlist":null,"socialMediaBanners":[{"id":10,"text":"У пропаганды есть миллиарды из бюджета. У нас — только вы","link":"","codename":"donation"},{"id":14,"text":"Список погибших и пропавших без вести российских военных","link":"","codename":"haron"},{"id":18,"text":"","link":"","codename":"mobile_app"},{"id":6,"text":"Оставайтесь на связи: подпишитесь на нашу рассылку","link":"","codename":"newsletter"}]}},"staticQueryHashes":["764694655"],"slicesMap":{}}