{"id":490,"date":"2023-02-16T15:41:41","date_gmt":"2023-02-16T15:41:41","guid":{"rendered":"https:\/\/tinyytopic.com\/?p=490"},"modified":"2023-02-16T15:20:48","modified_gmt":"2023-02-16T15:20:48","slug":"find-a-string-within-html-text","status":"publish","type":"post","link":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/","title":{"rendered":"Find a string within HTML text"},"content":{"rendered":"\n<div class=\"wp-block-uagb-advanced-heading uagb-block-49a11580\"><h5 class=\"uagb-heading-text\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><br>How to find string within an HTML text using the Ready-to-use function?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">Ready-to-use Python function to find a string in an HTML text:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def find_string_for_webscrap(HtmlTxt, first_char, last_char, start_location=1):\n    # Find a string from HTML text data\n    \n    output_char = 'Not found!'\n    EndLoc = 0\n    \n    InitLoc = HtmlTxt.find(first_char, start_location) + len(first_char)\n    if InitLoc &lt; len(first_char): return output_char, EndLoc\n    \n    EndLoc = HtmlTxt.find(last_char, InitLoc)\n    if EndLoc &lt; 1: return output_char, EndLoc\n    \n    output_char = HtmlTxt[InitLoc:EndLoc]\n    output_char = ' '.join(output_char.split()) # replace continues white spaces with single space\n    \n    return output_char, EndLoc<\/pre>\n\n\n\n<p style=\"font-size:15px\">Write your main code as a sample below,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HtmlTxt, status_code = webscrap(\"https:\/\/www.digikey.com\/en\/products\/detail\/vishay-dale\/CRCW1206100RFKEA\/1176530\", 30)\ntxt, EndLoc = find_string_for_webscrap(HtmlTxt, 'ref_part_description=', ';', 1)\nprint(txt)\ntxt, EndLoc = find_string_for_webscrap(HtmlTxt, 'ref_part_available', ';', EndLoc)\nprint(txt)<\/code><\/pre>\n\n\n\n<p style=\"font-size:15px\">The output of the code is,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RES SMD 100 OHM 1% 1\/4W 1206\n=<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-98122e1e\"><h5 class=\"uagb-heading-text\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">How does the function work?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">This Python function <code>find_string_for_webscrap<\/code> takes in four parameters:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>HtmlTxt<\/code> &#8211; a string that contains HTML text data that needs to be searched<\/li>\n\n\n\n<li><code>first_char<\/code> &#8211; a string that specifies the first character or sequence of characters that needs to be found<\/li>\n\n\n\n<li><code>last_char<\/code> &#8211; a string that specifies the last character or sequence of characters that needs to be found<\/li>\n\n\n\n<li><code>start_location<\/code> (optional) &#8211; an integer that specifies the starting location for the search. If not specified, the default value is 1.<\/li>\n<\/ol>\n\n\n\n<p style=\"font-size:15px\">The function first initializes the output character to &#8216;Not found!&#8217; and the end location to 0. It then searches for the starting location of the first character using the <code>find()<\/code> method, and adds the length of the first character to get the initial location. If the initial location is less than the length of the first character, it means that the first character was not found and the function returns the output character as &#8216;Not found!&#8217; and the end location as 0.<\/p>\n\n\n\n<p style=\"font-size:15px\">If the first character is found, the function searches for the end location of the last character using the <code>find()<\/code> method, starting from the initial location. If the end location is less than 1, it means that the last character was not found and the function returns the output character as &#8216;Not found!&#8217; and the end location as 0.<\/p>\n\n\n\n<p style=\"font-size:15px\">If both the first and last characters are found, the function extracts the substring between the initial and end locations using slicing and stores it in the output character. Finally, the function removes any continuous white spaces in the output character using <code>split()<\/code> and <code>join()<\/code> methods and returns the output character and end location.<\/p>\n\n\n\n<p style=\"font-size:15px\">Overall, this function searches for a substring in a given HTML text data between two specified characters, and returns the substring and the end location.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ready-to-use Python function to find a string in an HTML text: Write your main code as a sample below, The output of the code is, This Python function find_string_for_webscrap takes in four parameters: The function first initializes the output character to &#8216;Not found!&#8217; and the end location to 0. It then searches for the starting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12,17],"tags":[33,14,13,16,15],"class_list":["post-490","post","type-post","status-publish","format-standard","hentry","category-python","category-useful-function","tag-find-string","tag-programming-language","tag-python","tag-python-code","tag-python-sample-code"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Find a string within HTML text - tinyytopic.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Find a string within HTML text - tinyytopic.com\" \/>\n<meta property=\"og:description\" content=\"Ready-to-use Python function to find a string in an HTML text: Write your main code as a sample below, The output of the code is, This Python function find_string_for_webscrap takes in four parameters: The function first initializes the output character to &#8216;Not found!&#8217; and the end location to 0. It then searches for the starting [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/\" \/>\n<meta property=\"og:site_name\" content=\"tinyytopic.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-16T15:41:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-16T15:20:48+00:00\" \/>\n<meta name=\"author\" content=\"tinyytopic.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tinyytopic.com\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/\",\"url\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/\",\"name\":\"Find a string within HTML text - tinyytopic.com\",\"isPartOf\":{\"@id\":\"https:\/\/tinyytopic.com\/#website\"},\"datePublished\":\"2023-02-16T15:41:41+00:00\",\"dateModified\":\"2023-02-16T15:20:48+00:00\",\"author\":{\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tinyytopic.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Find a string within HTML text\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tinyytopic.com\/#website\",\"url\":\"https:\/\/tinyytopic.com\/\",\"name\":\"tinyytopic.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tinyytopic.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\",\"name\":\"tinyytopic.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"caption\":\"tinyytopic.com\"},\"sameAs\":[\"http:\/\/tinyytopic.com\"],\"url\":\"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Find a string within HTML text - tinyytopic.com","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:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/","og_locale":"en_US","og_type":"article","og_title":"Find a string within HTML text - tinyytopic.com","og_description":"Ready-to-use Python function to find a string in an HTML text: Write your main code as a sample below, The output of the code is, This Python function find_string_for_webscrap takes in four parameters: The function first initializes the output character to &#8216;Not found!&#8217; and the end location to 0. It then searches for the starting [&hellip;]","og_url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/","og_site_name":"tinyytopic.com","article_published_time":"2023-02-16T15:41:41+00:00","article_modified_time":"2023-02-16T15:20:48+00:00","author":"tinyytopic.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"tinyytopic.com","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/","url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/","name":"Find a string within HTML text - tinyytopic.com","isPartOf":{"@id":"https:\/\/tinyytopic.com\/#website"},"datePublished":"2023-02-16T15:41:41+00:00","dateModified":"2023-02-16T15:20:48+00:00","author":{"@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb"},"breadcrumb":{"@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/16\/find-a-string-within-html-text\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tinyytopic.com\/"},{"@type":"ListItem","position":2,"name":"Find a string within HTML text"}]},{"@type":"WebSite","@id":"https:\/\/tinyytopic.com\/#website","url":"https:\/\/tinyytopic.com\/","name":"tinyytopic.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tinyytopic.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb","name":"tinyytopic.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","caption":"tinyytopic.com"},"sameAs":["http:\/\/tinyytopic.com"],"url":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"tinyytopic.com","author_link":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"},"uagb_comment_info":105,"uagb_excerpt":"Ready-to-use Python function to find a string in an HTML text: Write your main code as a sample below, The output of the code is, This Python function find_string_for_webscrap takes in four parameters: The function first initializes the output character to &#8216;Not found!&#8217; and the end location to 0. It then searches for the starting&hellip;","_links":{"self":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/comments?post=490"}],"version-history":[{"count":3,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/490\/revisions"}],"predecessor-version":[{"id":492,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/490\/revisions\/492"}],"wp:attachment":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/media?parent=490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/categories?post=490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/tags?post=490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}