title: I’d like to scrape the text in nested elements within multiple divs
body: <p>Using BeautifulSoup4 for python3 I’d like to scrape the text in nested elements within divs. But first, I want to extract links also nested in elements within divs.</p>
  
  <p>How would I go about grabbing a link <code>LINK-I-WANT.COM</code> and an image <code>IMAGE-I-WANT.JPG</code> nested in something like this:</p>
  
  <pre><code>&lt;section class="LINK_CLASS"&gt;
      &lt;div class="LINK_CLASS2"&gt;
          &lt;div class="LINK_CLASS3"&gt;
              &lt;span class="#"&gt;random text&lt;/span&gt;
              &lt;a href="LINK-I-WANT.COM"&gt;
                  &lt;img  src="IMAGE-I-WANT.JPG" class="IMG_CLASS"/&gt;
              &lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  </code></pre>
  
  <p>All the links scraped would then be saved to a list and the, the script will go through each link and find something a long the lines of:</p>
  
  <pre><code>&lt;div class=“CLASS_ONE”&gt;
      &lt;div class=“CLASS_TWO”&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;span&gt;FOO&lt;/span&gt;BAR&lt;/li&gt;
          &lt;li&gt;&lt;span&gt;FOO2&lt;/span&gt;BAR2&lt;/li&gt;
          &lt;li&gt;&lt;span&gt;FOO3&lt;/span&gt;BAR3&lt;/li&gt;
          &lt;li&gt;&lt;span&gt;FOO4&lt;/span&gt;BAR4&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Using the example above, how would I access the <code>FOO#</code> and <code>BAR#</code> so that when I loop through every link and find the information that each page has (FOO# &amp; BAR#), I can print it to the generated text file, for every link?</p>
  
  <p>Do forgive me if I am making no sense. Here is my attempt at the code, I would greatly appreciate any help.</p>
  
  <pre><code>def spider(max_pages):
      page = 1
      subs = []
      print("Getting links...")
      while page &lt;= max_pages:
          url = "http://example.com" 
          source_code = requests.get(url)
          plain_text = source_code.text
          soup = BeautifulSoup(plain_text, "html.parser")
          for link in soup.findAll("section",{"class":"LINK_CLASS"}):
  </code></pre>
  
  <p>This is the part where I get stuck... if the <code>&lt;a&gt;</code> tag had a class, this would be a lot easier, unfortunately, the <code>&lt;a&gt;</code> tag just has a href, so I have to try and access it by pointing to other elements. I don't know how to look for an element within an element, could someone please help me?</p>