Python: extract text from string
<p>I try to extract text from url request, but not all dict contain key with text, and when I try to use <code>{k: v[0] for k, v in parse_qs(str).items()}</code> to urls, I lose a lot of requests, so I try <code>str = urllib.unquote(u[0])</code>.
After that I get strings like</p>

<pre><code>ÑÐ¼Ð¾ÑÑÐµÑÑ Ð»ÑÑÑÐµ Ð½Ðµ Ð±ÑÐ²Ð°ÐµÑ&amp;clid=1955453&amp;win=176
Jade+Jantzen&amp;ie=utf-8&amp;oe=utf-8&amp;gws_rd=cr&amp;ei=FQB0V9WbIoahsAH5zZGACg
ÐºÐ°Ðº+ÑÐºÑÑÑÑ+Ð»Ð¾Ð¿Ð¾ÑÑÐ¾ÑÑÑ&amp;newwindow=1&amp;biw=1366&amp;bih=657&amp;source=lnms&amp;sa=X&amp;sqi=2&amp;pjf=1&amp;ved=0ahUKEwju5cPJy83NAhUPKywKHVHXBesQ_AUICygA&amp;dpr=1
ÑÐ¼Ð¾ÑÑÐµÑÑ Ð»ÑÑÑÐµ Ð½Ðµ Ð±ÑÐ²Ð°ÐµÑ&amp;clid=1955453&amp;win=176
2&amp;clid=1976874&amp;win=85&amp;msid=1467228292.64946.22901.24595&amp;text=ÐºÐ°Ðº Ð²ÑÐ±ÑÐ°ÑÑ ÑÐ¼Ð°ÑÑÑÐ¾Ð½
Ð¼Ð°ÑÐºÐ°Ð¸ Ð³ÐµÐ¹Ð»Ð°&amp;lr=10750&amp;clid=1985551-210&amp;win=213
</code></pre>

<p>And I want to get</p>

<pre><code>ÑÐ¼Ð¾ÑÑÐµÑÑ Ð»ÑÑÑÐµ Ð½Ðµ Ð±ÑÐ²Ð°ÐµÑ
Jade Jantzen
ÐºÐ°Ðº ÑÐºÑÑÑÑ Ð»Ð¾Ð¿Ð¾ÑÑÐ¾ÑÑÑ
ÑÐ¼Ð¾ÑÑÐµÑÑ Ð»ÑÑÑÐµ Ð½Ðµ Ð±ÑÐ²Ð°ÐµÑ
ÐºÐ°Ðº Ð²ÑÐ±ÑÐ°ÑÑ ÑÐ¼Ð°ÑÑÑÐ¾Ð½
Ð¼Ð°ÑÐºÐ°Ð¸ Ð³ÐµÐ¹Ð»Ð°
</code></pre>

<p>Is any way to extract that?</p>

<p>Just split by <code>&amp;</code> and take the first part:</p>

<pre><code>txt = urllib.unquote(u[0]).split("&amp;")[0]
</code></pre>

<p>And don't use <code>str</code> as a variable name - it's a built-in type name in Python.</p>

<p><strong>EDIT:</strong>
Unfortunatelly this <code>2&amp;clid=1976874&amp;win=85&amp;msid=1467228292.64946.22901.24595&amp;text=ÐºÐ°Ðº Ð²ÑÐ±ÑÐ°ÑÑ ÑÐ¼Ð°ÑÑÑÐ¾Ð½</code> line has a different pattern than the others. There's no common way to handle this one together with the others. I was tempted to use regex to match Cyrillic characters but <code>Jade Jantzen</code> wouldn't match. So for this one line, where the desired text is at the end, something like</p>

<pre><code>txt = urllib.unquote(u[0]).split("=")[-1]
</code></pre>

<p>would work. Still you didn't provide any actual criteria for desired text. As humans we can say how to transform what you get into what you want from this specific sample. But without clear rules of what to match, we can't provide a complete solution.</p>

<p><em>I'm aware that some (again some) of the lines have <code>"+"</code> in place of <code>" "</code>. This can possibly be solved with <code>.replace("+", " ")</code>.</em></p>

python regex urlparse