title: How to use re module in python to extract information?
body: <p>I write a little script to use collins website for translation. heres my code:</p>
  
  <pre><code>import urllib.request
  import re
  def translate(search):
      base_url = 'http://www.collinsdictionary.com/dictionary/american/'
      url = base_url + search
      p = urllib.request.urlopen(url).read()
      f = open('t.txt', 'w+b')
      f.write(p)
      f.close()
      f = open('t.txt', 'r')
      t = f.read()
      m = re.search(r'(&lt;span class="def"&gt;)(\w.*)(&lt;/span&gt;]*)',t)
      n = m.group(2)
      print(n)
      f.close()
  </code></pre>
  
  <p>I have some questions:</p>
  
  <ol>
  <li><p>I can't use re.search on p. it raises this error: 
  <code>TypeError: can't use a string pattern on a bytes-like object</code>
  is there a way to use <code>re.search</code> without saving it?</p></li>
  <li><p>After saving file I should reopen it to use re.search otherwise it raises this error: <code>TypeError: must be str, not bytes</code> why this error happens?</p></li>
  <li><p>in this program I want to extract information between <code>&lt;span class="def"&gt; and &lt;/span&gt;</code> from first match. but pattern that I wrote not work good in all cases. for example translate('three') is good. out put is : "totaling one more than two" but for translate('tree') out put is:
  "a treelike bush or shrub &nbsp; â‡’&nbsp;a rose tree"
  is there a way to correct this pattern. regular expression or any other tools?</p></li>
  </ol>