Python Regular Expression Finding a Specific Text Between Headers
<p>I'm just starting to learn about regular expressions in Python, and I've made a bit of progress on what I want to get done.</p>

<pre><code>import urllib.request
import urllib.parse
import re

x = urllib.request.urlopen("http://www.SOMEWEBSITE.com")
contents = x.read()

paragraphs = re.findall(r'&lt;p&gt;(.*?)&lt;/p&gt;', str(contents))
</code></pre>

<p>So with that regular expression I'm able to find everything between the paragraph headers, but what if I want to find paragraphs with specific words in them? For example, parse all paragraphs that have the word "cat" in them. I know that (.*?) find everything, but I'm just a bit lost on the intuition on finding a paragraph with a specific keyword. </p>

<p>Anyway, thanks.</p>

<p>It's better to use BeautifulSoup. Example:</p>

<pre><code>import urllib2
html = urllib2.urlopen("http://www.SOMEWEBSITE.com").read()
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html)

# now you can search the soup
</code></pre>

<p><strong>Documentation:</strong></p>

<p><a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/" rel="nofollow">BeautifulSoup Doc</a></p>

<p>But... if regex has to be used:</p>

<pre><code>&gt;&gt;&gt; str = "&lt;p&gt;This is some cat in a paragraph.&lt;/p&gt;"
&gt;&gt;&gt; re.findall(r'&lt;p&gt;(.*cat.*)&lt;/p&gt;', str)
['This is some cat in a paragraph.']
</code></pre>

python regex