title: Keyerror with bs4: return self.attrs[key]
body: <p>I have some code that was working, but recently started giving me an error. The problematic section of code looks like this:</p>
  <pre><code>if(new_hash != old_hash):
          print(new_hash)
          print(old_hash)
          # Finds content of the most recent post on the list
          content = BeautifulSoup(vf_html.find('description').findNext('description').find(text=lambda t: isinstance(t, CData)), 'html.parser')
          for img in content.select('img'):
              img.replace_with(img['alt'])
          content = content.text
          new_content_hash = hashlib.md5(str(content).encode('utf-8')).hexdigest()
          toSend = (content[:1000] + '') if len(content) &gt; 75 else content
          # Finds author of the most recent post on the list
          author = vf_xml.find('creator').get_text(strip=True)
          author = author.split()[0]
          author = author[1:]
  </code></pre>
  <p>This was working fine, but a few hours ago it starting throwing me this error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\Taran Mayer\Desktop\CodyBot\scrape.py&quot;, line 160, in &lt;module&gt;
      scrape()
    File &quot;C:\Users\Taran Mayer\Desktop\CodyBot\scrape.py&quot;, line 83, in scrape
      img.replace_with(img['alt'])
    File &quot;C:\Python38\lib\site-packages\bs4\element.py&quot;, line 1401, in __getitem__
      return self.attrs[key]
  KeyError: 'alt'
  </code></pre>
  <p>I don't think I changed anything, and I tried reverting to an earlier, working version of the code, but the error persisted. Can anybody help me find what I'm doing wrong? If I comment out the lines</p>
  <pre><code>for img in content.select('img'):
      img.replace_with(img['alt'])
  </code></pre>
  <p>the program works, but doesn't do what I want it to.</p>