finding a special path string in HTML text in python
<p>I'm trying to extract a path in an HTML file that I read.
In this case the path that I'm looking for is a logo from google's main site.</p>

<p>I'm pretty sure that the regular expression I defined is right, but I guess I'm missing something.</p>

<p>The code is:</p>

<pre><code>import re
import urllib
a=urllib.urlopen ('https://www.google.co.il/')
Text = a.read(250)
print Text
print '\n\n'
b= re.search (r'\"\/[a-z0-9 ]*',Text)

print format(b.group(0))
</code></pre>

<p>The actual text that I want to get is:</p>

<p><strong>/images/branding/googleg/1x/googleg_standard_color_128dp.png</strong></p>

<p>I'd really appreciate it if someone could point me in the right direction</p>

<p>Here's my answer:</p>

<pre><code>import re
import urllib

a=urllib.urlopen ('https://www.google.co.il/')
text = a.read(250)
print text
print '\n\n'
b= re.search (r'\"(\/[a-z0-9_. ]+)+\"',text)


print format(b.group(0))
</code></pre>

<p>Run gives:</p>

<pre><code>&gt;&gt;&gt; python stackoverflow.py
&lt;!doctype html&gt;&lt;html dir="rtl" itemscope="" itemtype="http://schema.org/WebPage" lang="iw"&gt;&lt;head&gt;&lt;meta content="text/html; charset=UTF-8" http-equiv="Content-Type"&gt;&lt;meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop=



"/images/branding/googleg/1x/googleg_standard_color_128dp.png"
</code></pre>

<p>Explanation o the regex <code>\"(\/[a-z0-9_. ]+)+\"</code> : firstly, in the string name of the picture, you miss <code>.</code> and <code>_</code>. You need to add these two into the square brackets as well since they appear in the path. <code>\/[a-z0-9_. ]+</code> matches a patter with <code>/</code> followed by some string wtih length at least 1. <code>(\/[a-z0-9_. ]+)+</code> replicates the previous match to allow for multiple matches of paths that have more than 1 folders. Finally, you add the two <code>"</code> at the beginning and end.</p>

python regex expression