Match URLs by file path and GET parameters (but not their values)
<p>How can I check if any of my list of URLs match the given <code>url</code>? I need URLs to match only if all GET parameter names (not their values) and the path are the same. For example, I have this list:</p>

<pre><code>links = [
  "http://example.com/page.php?param1=111&amp;param2=222",
  "http://example.com/page2.php?param1=111&amp;param2=222",
  "http://example.com/page2.php?param1=111&amp;param2=222&amp;someParameterN=NumberN"
  ]

url = "http://example.com/page2.php?param1=NOT111&amp;param2=NOT222"
</code></pre>

<p>This example is <code>True</code> because <code>url</code> matches <code>links[1]</code>. But how to match it in the most efficient way? I don't know what <code>url</code> will looks like.</p>

<p>I think that <a href="https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse" rel="nofollow"><code>urllib.parse.urlparse()</code></a> (if you are using Python 3) will help you, or <code>urlparse.urlparse()</code> for Python 2.</p>

<p>This function will break the URL up into its various components. Then you can compare all components, or a subset of them as you require. Example (Python 3)</p>

<pre><code>&gt;&gt;&gt; from urllib.parse import urlparse
&gt;&gt;&gt; urlparse('http://example.com/page.php?param1=111&amp;param2=222')
ParseResult(scheme='http', netloc='example.com', path='/page.php', params='', query='param1=111&amp;param2=222', fragment='')


&gt;&gt;&gt; url1 = urlparse('http://example.com/page.php?param1=111&amp;param2=222')
&gt;&gt;&gt; url2 = urlparse('http://example.com/page.php?param1=111&amp;param2=222')
&gt;&gt;&gt; url1 == url2
True

&gt;&gt;&gt; url3 = urlparse('http://example.com/page2.php?param2=222&amp;param1=111')
&gt;&gt;&gt; url1 == url3
False
&gt;&gt;&gt; url1.query == url3.query    # same GET params but in different order
False
</code></pre>

<p>The last example shows that the order of parameters in the query string affects the comparison. You can account for that by using <code>urllib.parse.parse_qs()</code>:</p>

<pre><code>&gt;&gt;&gt; from urllib.parse import parse_qs
&gt;&gt;&gt; parse_qs(url1.query)
{'param2': ['222'], 'param1': ['111']}
&gt;&gt;&gt; parse_qs(url1.query) == parse_qs(url3.query)
True
</code></pre>

<p>You can use the <code>.path</code> attribute of the <code>ParseResult</code> to compare "pages".</p>

<p>As I said, I think that will help you, however, I don't fully understand exactly what you are trying to do.</p>

python regex list url match