<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Beautifulsoup If Else Statement in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754614#M58184</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;"Grab and Go" isn't being found 3 times but 2 times while "grab and go" is being found 1 time.&amp;nbsp; The searches you are doing are case sensitive.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 02 Apr 2020 12:39:32 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2020-04-02T12:39:32Z</dc:date>
    <item>
      <title>Beautifulsoup If Else Statement</title>
      <link>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754611#M58181</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The &lt;CODE&gt;if else&lt;/CODE&gt; statement is sending an email no matter what I use in the &lt;CODE&gt;if&lt;/CODE&gt; part. I'm not sure what the problem is. I tried a bunch of different operators, namely &lt;CODE&gt;&amp;lt;3, &amp;lt;=3, &amp;lt;2, == -3, == 3&lt;/CODE&gt;.&lt;/P&gt;&lt;P&gt;(1.) I wanted to check the number of times a given word occurred in the HTML. (2.) If it occurred equal to or less than the number given, stop. Anything else, proceed (the script goes on to send an email out).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="string token"&gt;'''Checks school websites for changes in meal assistance during C19 pandemic'''&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# Import requests (to download the page)&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; requests
&lt;SPAN class="comment token"&gt;# Import BeautifulSoup (to parse what we download)&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; bs4 &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; BeautifulSoup
&lt;SPAN class="comment token"&gt;# Import win32com (to allow email)&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; win32com&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;client
&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; win32com&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;client &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; Dispatch&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; constants
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; re

&lt;SPAN class="keyword token"&gt;while&lt;/SPAN&gt; &lt;SPAN class="token boolean"&gt;True&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#set the url&lt;/SPAN&gt;
     url &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'https://manteno5.org/news/what_s_new/c_o_v_i_d-19_updates'&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#set the headers like we are a browser&lt;/SPAN&gt;
    headers &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'User-Agent'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 ( KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# download the homepage&lt;/SPAN&gt;
    response &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; requests&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;get&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;url&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; headers&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;headers&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;# parse the downloaded homepage and grab all text&lt;/SPAN&gt;
    soup &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; BeautifulSoup&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;response&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;text&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"lxml"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#the word I'm looking for&lt;/SPAN&gt;
    searched_word &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'Grab and Go'&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#find how many times the word occurs&lt;/SPAN&gt;
    results &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; soup&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;body&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;find_all&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;text&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;re&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;compile&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'{0}'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;searched_word&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; recursive&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token boolean"&gt;True&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'Found the word "{0}" {1} time(s)\n'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;searched_word&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; len&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;results&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#if the number of times "Grab and Go" occurs on the page is less &lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#than a given number print "No change"&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;soup&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;find&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;searched_word&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;==&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;3&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"No change"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
        &lt;SPAN class="keyword token"&gt;continue&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#but if the word "Grab and Go" occurs any other number of times&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;else&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
         &lt;SPAN class="comment token"&gt;#script goes on to send an email...&lt;/SPAN&gt;
        &lt;SPAN class="keyword token"&gt;break&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It sent the email even though "Grab and Go" only occurred three times? Print out:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;Found the word "Grab and Go" 3 time(s)‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:00:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754611#M58181</guid>
      <dc:creator>JaredPilbeam2</dc:creator>
      <dc:date>2021-12-12T08:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: Beautifulsoup If Else Statement</title>
      <link>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754612#M58182</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;if len(results) &amp;lt;= 3:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;do stuff&lt;/P&gt;&lt;P&gt;from the line that did the printing, you got the 'len' of results already&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Apr 2020 07:57:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754612#M58182</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2020-04-02T07:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Beautifulsoup If Else Statement</title>
      <link>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754613#M58183</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The Python str.find() method doesn't return the number of occurrences, it returns the lowest index of the substring.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Apr 2020 12:34:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754613#M58183</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-04-02T12:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: Beautifulsoup If Else Statement</title>
      <link>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754614#M58184</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;"Grab and Go" isn't being found 3 times but 2 times while "grab and go" is being found 1 time.&amp;nbsp; The searches you are doing are case sensitive.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Apr 2020 12:39:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754614#M58184</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-04-02T12:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: Beautifulsoup If Else Statement</title>
      <link>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754615#M58185</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I did notice that the bs4 search was case sensitive. I've actually looked into ways to deal with that, but forgot about it. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I used Dan's approach and it worked. But I had to take it out of the while loop because it was running endlessly.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; len&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;results&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;&amp;lt;=&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;3&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;

     do stuff&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:00:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/beautifulsoup-if-else-statement/m-p/754615#M58185</guid>
      <dc:creator>JaredPilbeam2</dc:creator>
      <dc:date>2021-12-12T08:00:55Z</dc:date>
    </item>
  </channel>
</rss>

