Python strip Google Alerts URL
<p>I've currently got a dataframe filled with Google Alert URLS that look like:</p>

<pre><code>link = 'https://www.google.com/url?rct=j&amp;sa=t&amp;url=http://3dprint.com/4353/littledlper-dlp-3d-printer-kickstarter/&amp;ct=ga&amp;cd=CAEYBCoSODQ1OTg1ODMwMzQwNDUzMTUxMhw2NTFlMTg3MTI1ZGE4Nzc3OmNvLnVrOmVuOkdC&amp;usg=AFQjCNF0HOEhqIZHEpdkH1eVdXt-JRBF3Q'
</code></pre>

<p>and I just want the part following <code>url=</code> and before the junk.</p>

<pre><code>http://3dprint.com/4353/littledlper-dlp-3d-printer-kickstarter/
</code></pre>

<p>I used <code>urllib.parse.urlparse(link)</code> to get a list of URL elements...</p>

<pre><code>parsed = ParseResult(scheme='https', netloc='www.google.com', path='/url', params='', query='rct=j&amp;sa=t&amp;url=http://3dprint.com/4353/littledlper-dlp-3d-printer-kickstarter/&amp;ct=ga&amp;cd=CAEYBCoSODQ1OTg1ODMwMzQwNDUzMTUxMhw2NTFlMTg3MTI1ZGE4Nzc3OmNvLnVrOmVuOkdC&amp;usg=AFQjCNF0HOEhqIZHEpdkH1eVdXt-JRBF3Q', fragment='')
</code></pre>

<p>but even then <code>parsed[4]</code> only breaks it down to...</p>

<pre><code>'rct=j&amp;sa=t&amp;url=http://3dprint.com/4353/littledlper-dlp-3d-printer-kickstarter/&amp;ct=ga&amp;cd=CAEYBCoSODQ1OTg1ODMwMzQwNDUzMTUxMhw2NTFlMTg3MTI1ZGE4Nzc3OmNvLnVrOmVuOkdC&amp;usg=AFQjCNF0HOEhqIZHEpdkH1eVdXt-JRBF3Q'
</code></pre>

<p>I found <a href="http://stackoverflow.com/questions/39088532/i-need-to-strip-a-google-alerts-url">other</a> <a href="http://stackoverflow.com/questions/15777513/extract-original-url-from-google-alerts-link">queries</a> on Stack with this same question but they were in other programming languages than Python.</p>

<p>Any ideas on a Python approach?</p>

<p>You may use a regex on <code>parsed[4]</code> to extract that URL:</p>

<pre><code>(?:^|&amp;)url=([^&amp;]+)
</code></pre>

<p>See the <a href="https://regex101.com/r/sG7yY1/1" rel="nofollow">regex demo</a></p>

<p><em>Details</em>:</p>

<ul>
<li><code>(?:^|&amp;)</code> - either start of string or <code>&amp;</code></li>
<li><code>url=</code> - literal text <code>url=</code></li>
<li><code>([^&amp;]+)</code> - Group 1 capturing one or more symbols other than <code>&amp;</code>.</li>
</ul>

<p><a href="https://ideone.com/BtRIlP" rel="nofollow">Python demo</a>:</p>

<pre><code>import re
p = re.compile(r'(?:^|&amp;)url=([^&amp;]+)')
s = "rct=j&amp;sa=t&amp;url=http://3dprint.com/4353/littledlper-dlp-3d-printer-kickstarter/&amp;ct=ga&amp;cd=CAEYBCoSODQ1OTg1ODMwMzQwNDUzMTUxMhw2NTFlMTg3MTI1ZGE4Nzc3OmNvLnVrOmVuOkdC&amp;usg=AFQjCNF0HOEhqIZHEpdkH1eVdXt-JRBF3Q"
mObj = p.search(s)
if mObj:
    print(mObj.group(1))
</code></pre>

python regex urllib2