{"componentChunkName":"component---src-templates-material-js","path":"/workshops/2021/06/05/pishem-svoego-robota-na-python-urok-7/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":273,"header":"Пишем своего робота на Python. Урок 7","lead":"Боремся с ошибками, которые могут сломать выполнение нашей функции","category":"workshops","entry_image":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-1920x960px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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/2dccd2549d9d4c698c9b2d4064fd5663-1920x960px_1x.png","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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/2dccd2549d9d4c698c9b2d4064fd5663-1272x636px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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/2dccd2549d9d4c698c9b2d4064fd5663-1272x636px_1x.png","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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/2dccd2549d9d4c698c9b2d4064fd5663-912x456px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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/2dccd2549d9d4c698c9b2d4064fd5663-912x456px_1x.png","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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/2dccd2549d9d4c698c9b2d4064fd5663-600x400px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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/2dccd2549d9d4c698c9b2d4064fd5663-600x400px_1x.png","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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/2dccd2549d9d4c698c9b2d4064fd5663-360x360px_1x.webp","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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/2dccd2549d9d4c698c9b2d4064fd5663-360x360px_1x.png","url_2x":"https://static.istories.media/uploaded/images/2dccd2549d9d4c698c9b2d4064fd5663-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":"Двигаемся дальше в разработке нашего робота по поиску госконтрактов. Тема сегодняшнего урока — ошибки. ","type":"p","className":""},{"data":{"id":2462,"data":{"url":"https://www.youtube.com/embed/xlbZyRdg62Y","text":"Видео: Глеб Лиманский","provider":"youtube","rawInput":"https://www.youtube.com/watch?v=xlbZyRdg62Y","ratio_mobile":"16:9","ratio_desktop":"16:9"},"category":"video-embed"},"type":"video-embed"},{"data":"Если в нашем запросе будут указаны параметры, по которым ничего не находится, или сервис «ГосЗатрат»&nbsp;перестанет отвечать на запросы, мы получим ошибку — и тогда наш скрипт сломается. Чтобы этого избегать, мы будем применять синтаксис <strong>try-except</strong>. Давайте попробуем для начала воспроизвести ошибку.","type":"p","className":""},{"data":{"id":2459,"data":{"images":[{"credit":"Меняем год в запросе на еще не наступивший 2022...","imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/1ce8bb350fa84fc99bfa3c83970bc81d-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/1ce8bb350fa84fc99bfa3c83970bc81d-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":296,"real_height_2x":592},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/1ce8bb350fa84fc99bfa3c83970bc81d-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/1ce8bb350fa84fc99bfa3c83970bc81d-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":296,"real_height_2x":592}]},{"credit":"...и код ломается","imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/2d7eb0765d8a4b319cf348e07eb8ebf7-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/2d7eb0765d8a4b319cf348e07eb8ebf7-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":340,"real_height_2x":680},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/2d7eb0765d8a4b319cf348e07eb8ebf7-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/2d7eb0765d8a4b319cf348e07eb8ebf7-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":340,"real_height_2x":680}]}]},"category":"two-images-embed"},"type":"two-images-embed"},{"data":"Разберемся, как не получать такие ошибки. С помощью нового условия как бы скажем нашей функции: попробуй сделать запрос (<strong>try</strong>), а если не получается (<strong>except</strong>), сделай что-то другое. Что именно? Например, можем вывести некоторое сообщение: «Что-то пошло не так!».","type":"p","className":""},{"data":{"id":2460,"data":{"images":[{"credit":"Теперь, если наше условие будет выполняться без ошибок, мы получим нужный результат... ","imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/664724f3953247bdb1f57904edafde87-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/664724f3953247bdb1f57904edafde87-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":427,"real_height_2x":854},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/664724f3953247bdb1f57904edafde87-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/664724f3953247bdb1f57904edafde87-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":427,"real_height_2x":854}]},{"credit":"...а если не будет — получим сообщение об ошибке","imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/ee13b5a97f0a417896619b6feea826fe-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/ee13b5a97f0a417896619b6feea826fe-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":355,"real_height_2x":710},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/ee13b5a97f0a417896619b6feea826fe-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/ee13b5a97f0a417896619b6feea826fe-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":355,"real_height_2x":710}]}]},"category":"two-images-embed"},"type":"two-images-embed"},{"data":"С помощью этого синтаксиса можно прописывать конкретно, какой ошибки мы хотим избежать. Сейчас в нашем коде вне зависимости от ошибки мы получим сообщение «Что-то пошло не так!». Та ошибка, когда мы сами ввели неподходящее значение параметра, называется <strong>JSONDecodeError</strong>.&nbsp;","type":"p","className":""},{"data":{"id":2461,"data":{"images":[{"imagesList":[{"format":"webp","url_1x":"https://static.istories.media/uploaded/images/f94af2e028034070ad00eb8e6a1ec387-1200xautopx_1x.webp","url_2x":"https://static.istories.media/uploaded/images/f94af2e028034070ad00eb8e6a1ec387-1200xautopx_2x.webp","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":402,"real_height_2x":804},{"format":"png","url_1x":"https://static.istories.media/uploaded/images/f94af2e028034070ad00eb8e6a1ec387-1200xautopx_1x.png","url_2x":"https://static.istories.media/uploaded/images/f94af2e028034070ad00eb8e6a1ec387-1200xautopx_2x.png","for_width":1200,"for_height":"auto","real_width_1x":1200,"real_width_2x":2400,"real_height_1x":402,"real_height_2x":804}]}]},"category":"image-embed"},"type":"image-embed"},{"data":"Если мы добавим ее название в условие, а вылетит какая-то другая — наша функция снова сломается. В этот раз мы оставим уточнение, чтобы в дальнейшем иметь возможность отследить другие ошибки и решить, как с ними поступить. ","type":"p","className":""}],"created_at_date":"2021-06-05","updated_at_date":null,"created_at_date_time":null,"updated_at_date_time":null,"is_exclusive":false,"is_urgent":false,"initially_published_at":"2021-06-05T08:23:57.943Z","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/06/05/pishem-svoego-robota-na-python-urok-7/","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":273,"topics":[],"meta":{"og_title":"","og_description":"","twitter_title":"","twitter_description":"","og_image":"https://static.istories.media/uploaded/images/share/a87fcf15a1c54988a3c8430db64d927c.png","twitter_image":"https://static.istories.media/uploaded/images/share/a87fcf15a1c54988a3c8430db64d927c.png","vk_image":"https://static.istories.media/uploaded/images/share/vk/0f55a88c5f1546caab63f29635e6b03f.png"},"tags":["ddj","ошибки","python"],"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":{}}