title: Beautiful Soup Headers with table data
body: <p>I am scraping IMDB for the members of its cast (the IMDB API doesn't have comprehensive cast/credits data). The final product I want is a table with three columns that gets the data from all the tables in the webpage and sorts them like this:</p>
  
  <pre><code>Produced by | Gary Kurtz | producer 
  
  Produced by | George Lucas | executive producer
  
  Music by    | John Williams | 
  </code></pre>
  
  <p>(using star wars as an example, <a href="http://www.imdb.com/title/tt0076759/fullcredits?ref_=tt_cl_sm#cast" rel="nofollow noreferrer">http://www.imdb.com/title/tt0076759/fullcredits?ref_=tt_cl_sm#cast</a>)</p>
  
  <p>The following code is almost there, but there is a ton of unnecessary whitespace, and the .parent function is surely being used wrong. What is the best way to find the value of the h4 above a table?</p>
  
  <p>Here's the code. </p>
  
  <pre><code> with open(fname, 'r') as f:
          soup = BeautifulSoup(f.read(),'html5lib')
          soup.prettify()
  
  
          with open(fname, 'r') as f:
          soup = BeautifulSoup(f.read(),'html5lib')
          soup.prettify()
  
          for child in soup.find_all('td',{'class':'name'}):
              print child.parent.text, child.parent.parent.parent.parent.parent.parent.text.encode('utf-8')
  </code></pre>
  
  <p>I'm trying to get the values such as "Directed by" from these h4 headers</p>