{"id":667,"date":"2023-08-18T04:20:00","date_gmt":"2023-08-18T04:20:00","guid":{"rendered":"https:\/\/henryov.com\/ian\/?p=667"},"modified":"2024-10-06T13:09:15","modified_gmt":"2024-10-06T13:09:15","slug":"parsing-with-python-cont","status":"publish","type":"post","link":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/","title":{"rendered":"Parsing with python cont&#8230;."},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"667\" class=\"elementor elementor-667\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-563c6fc elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"563c6fc\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-27cd809\" data-id=\"27cd809\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-9ccfb2a elementor-widget elementor-widget-heading\" data-id=\"9ccfb2a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">This is a copy of my code. I don't have my github set up. <\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-f692970 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"f692970\" data-element_type=\"section\" data-e-type=\"section\" data-settings=\"{&quot;background_background&quot;:&quot;classic&quot;}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-a8c56be\" data-id=\"a8c56be\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-47ff4fa elementor-widget__width-initial elementor-widget elementor-widget-text-editor\" data-id=\"47ff4fa\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>import imaplib<\/p><p>import email<\/p><p>import yaml<\/p><p>with open(&#8220;credentials.yml&#8221;) as f:<br \/>content = f.read()<\/p><p>my_credentials = yaml.load(content, Loader=yaml.FullLoader)<\/p><p>user, password = my_credentials[&#8220;user&#8221;], my_credentials[&#8220;password&#8221;]<\/p><p>imap_url = &#8216;imap.gmail.com&#8217;<\/p><p># Connection with GMAIL using SSL<br \/>my_mail = imaplib.IMAP4_SSL(imap_url)<\/p><p># Log in using your credentials<br \/>my_mail.login(user, password)<\/p><p># Select the Inbox to fetch messages<br \/>my_mail.select(&#8216;Inbox&#8217;)<\/p><p>#Define Key and Value for email search<br \/>key = &#8216;FROM&#8217;<br \/>value = &#8216;paypal@emails.paypal.com&#8217;<br \/>_, data = my_mail.search(None, key, value) #Search for emails with specific key and value<\/p><p>mail_id_list = data[0].split() #IDs of all emails that we want to fetch<\/p><p>msgs = [] # empty list to capture all messages<br \/>#Iterate through messages and extract data into the msgs list<br \/>for num in mail_id_list:<br \/>typ, data = my_mail.fetch(num, &#8216;(RFC822)&#8217;) #RFC822 returns whole message (BODY fetches just body)<br \/>msgs.append(data)<\/p><p># NOTE that a Message object consists of headers and payloads.<\/p><p>with open(&#8220;email_results.txt&#8221;, &#8220;w&#8221;, encoding=&#8221;utf-8&#8243;) as file:<\/p><p>for msg in msgs[::-1]:<br \/>for response_part in msg:<br \/>if isinstance(response_part, tuple): my_msg=email.message _from_bytes(response_part[1])<br \/>subject=my_msg[&#8216;subject&#8217;]<br \/>sender=my_msg[&#8216;from&#8217;]<br \/>body=&#8221;&#8221;<\/p><p>for part in my_msg.walk():<br \/>if part.get_content_type()== &#8216;text\/plain&#8217;:<br \/>body=part.get_payload( decode= True).decode(&#8220;utf-8&#8221;)<br \/>break # only get the first plain text response_part<\/p><p>#write email details to the file<br \/>file.write(&#8220;______________ _____________________\\n&#8221;)<br \/>file.write(&#8220;subj: &#8221; + str(subject) + &#8220;\\n&#8221;)<br \/>file.write(&#8220;from: &#8221; + str(sender) + &#8220;\\n&#8221;)<br \/>file.write(&#8220;body: &#8221; + str(body) + &#8220;\\n&#8221;)<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>This is a copy of my code. I don&#8217;t have my github set up. import imaplib import email import yaml with open(&#8220;credentials.yml&#8221;) as f:content = f.read() my_credentials = yaml.load(content, Loader=yaml.FullLoader) user, password = my_credentials[&#8220;user&#8221;], my_credentials[&#8220;password&#8221;] imap_url = &#8216;imap.gmail.com&#8217; # Connection with GMAIL using SSLmy_mail = imaplib.IMAP4_SSL(imap_url) # Log in using your credentialsmy_mail.login(user, password) # Select [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[31],"tags":[33,32],"class_list":["post-667","post","type-post","status-publish","format-standard","hentry","category-coding","tag-coding","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Parsing with python cont.... - I.A.N.<\/title>\n<meta name=\"description\" content=\"This is a continuation of the last post. Parsing with python shows how we can use a few lines of code to extract emails.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Parsing with python cont.... - I.A.N.\" \/>\n<meta property=\"og:description\" content=\"This is a continuation of the last post. Parsing with python shows how we can use a few lines of code to extract emails.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/\" \/>\n<meta property=\"og:site_name\" content=\"I.A.N.\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-18T04:20:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-06T13:09:15+00:00\" \/>\n<meta name=\"author\" content=\"henryoviedo93\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"henryoviedo93\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/parsing-with-python-cont\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/parsing-with-python-cont\\\/\"},\"author\":{\"name\":\"henryoviedo93\",\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/#\\\/schema\\\/person\\\/d600d28f4c5b331b316fde4ecc63b511\"},\"headline\":\"Parsing with python cont&#8230;.\",\"datePublished\":\"2023-08-18T04:20:00+00:00\",\"dateModified\":\"2024-10-06T13:09:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/parsing-with-python-cont\\\/\"},\"wordCount\":274,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/#organization\"},\"keywords\":[\"coding\",\"python\"],\"articleSection\":[\"coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/henryov.com\\\/ian\\\/parsing-with-python-cont\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/parsing-with-python-cont\\\/\",\"url\":\"https:\\\/\\\/henryov.com\\\/ian\\\/parsing-with-python-cont\\\/\",\"name\":\"Parsing with python cont.... - I.A.N.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/#website\"},\"datePublished\":\"2023-08-18T04:20:00+00:00\",\"dateModified\":\"2024-10-06T13:09:15+00:00\",\"description\":\"This is a continuation of the last post. Parsing with python shows how we can use a few lines of code to extract emails.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/parsing-with-python-cont\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/henryov.com\\\/ian\\\/parsing-with-python-cont\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/parsing-with-python-cont\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/henryov.com\\\/ian\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Parsing with python cont&#8230;.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/#website\",\"url\":\"https:\\\/\\\/henryov.com\\\/ian\\\/\",\"name\":\"I.A.N.\",\"description\":\"Independent analytical network\",\"publisher\":{\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/henryov.com\\\/ian\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/#organization\",\"name\":\"I.A.N.\",\"url\":\"https:\\\/\\\/henryov.com\\\/ian\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/henryov.com\\\/ian\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/cropped-signature-red-gray-background.png?fit=1415%2C511&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/henryov.com\\\/ian\\\/wp-content\\\/uploads\\\/2023\\\/08\\\/cropped-signature-red-gray-background.png?fit=1415%2C511&ssl=1\",\"width\":1415,\"height\":511,\"caption\":\"I.A.N.\"},\"image\":{\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/henryov.com\\\/ian\\\/#\\\/schema\\\/person\\\/d600d28f4c5b331b316fde4ecc63b511\",\"name\":\"henryoviedo93\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c97993610e494f0be5e96cd079c87672609aac53dbba616d4c71afdec877ff94?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c97993610e494f0be5e96cd079c87672609aac53dbba616d4c71afdec877ff94?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c97993610e494f0be5e96cd079c87672609aac53dbba616d4c71afdec877ff94?s=96&d=retro&r=g\",\"caption\":\"henryoviedo93\"},\"url\":\"https:\\\/\\\/henryov.com\\\/ian\\\/author\\\/henryoviedo93\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Parsing with python cont.... - I.A.N.","description":"This is a continuation of the last post. Parsing with python shows how we can use a few lines of code to extract emails.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/","og_locale":"en_US","og_type":"article","og_title":"Parsing with python cont.... - I.A.N.","og_description":"This is a continuation of the last post. Parsing with python shows how we can use a few lines of code to extract emails.","og_url":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/","og_site_name":"I.A.N.","article_published_time":"2023-08-18T04:20:00+00:00","article_modified_time":"2024-10-06T13:09:15+00:00","author":"henryoviedo93","twitter_card":"summary_large_image","twitter_misc":{"Written by":"henryoviedo93","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/#article","isPartOf":{"@id":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/"},"author":{"name":"henryoviedo93","@id":"https:\/\/henryov.com\/ian\/#\/schema\/person\/d600d28f4c5b331b316fde4ecc63b511"},"headline":"Parsing with python cont&#8230;.","datePublished":"2023-08-18T04:20:00+00:00","dateModified":"2024-10-06T13:09:15+00:00","mainEntityOfPage":{"@id":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/"},"wordCount":274,"commentCount":0,"publisher":{"@id":"https:\/\/henryov.com\/ian\/#organization"},"keywords":["coding","python"],"articleSection":["coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/henryov.com\/ian\/parsing-with-python-cont\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/","url":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/","name":"Parsing with python cont.... - I.A.N.","isPartOf":{"@id":"https:\/\/henryov.com\/ian\/#website"},"datePublished":"2023-08-18T04:20:00+00:00","dateModified":"2024-10-06T13:09:15+00:00","description":"This is a continuation of the last post. Parsing with python shows how we can use a few lines of code to extract emails.","breadcrumb":{"@id":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/henryov.com\/ian\/parsing-with-python-cont\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/henryov.com\/ian\/parsing-with-python-cont\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/henryov.com\/ian\/"},{"@type":"ListItem","position":2,"name":"Parsing with python cont&#8230;."}]},{"@type":"WebSite","@id":"https:\/\/henryov.com\/ian\/#website","url":"https:\/\/henryov.com\/ian\/","name":"I.A.N.","description":"Independent analytical network","publisher":{"@id":"https:\/\/henryov.com\/ian\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/henryov.com\/ian\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/henryov.com\/ian\/#organization","name":"I.A.N.","url":"https:\/\/henryov.com\/ian\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/henryov.com\/ian\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/henryov.com\/ian\/wp-content\/uploads\/2023\/08\/cropped-signature-red-gray-background.png?fit=1415%2C511&ssl=1","contentUrl":"https:\/\/i0.wp.com\/henryov.com\/ian\/wp-content\/uploads\/2023\/08\/cropped-signature-red-gray-background.png?fit=1415%2C511&ssl=1","width":1415,"height":511,"caption":"I.A.N."},"image":{"@id":"https:\/\/henryov.com\/ian\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/henryov.com\/ian\/#\/schema\/person\/d600d28f4c5b331b316fde4ecc63b511","name":"henryoviedo93","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c97993610e494f0be5e96cd079c87672609aac53dbba616d4c71afdec877ff94?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c97993610e494f0be5e96cd079c87672609aac53dbba616d4c71afdec877ff94?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c97993610e494f0be5e96cd079c87672609aac53dbba616d4c71afdec877ff94?s=96&d=retro&r=g","caption":"henryoviedo93"},"url":"https:\/\/henryov.com\/ian\/author\/henryoviedo93\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/posts\/667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/comments?post=667"}],"version-history":[{"count":8,"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/posts\/667\/revisions"}],"predecessor-version":[{"id":911,"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/posts\/667\/revisions\/911"}],"wp:attachment":[{"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/media?parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/categories?post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/henryov.com\/ian\/wp-json\/wp\/v2\/tags?post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}