What do you do when you catch a Requests exception and you want to identify the URL that caused it? This script loops through a list of URLs and scrapes certain text. If one of the URLs fail in any way I catch it the Try Except block. All I can do now is print the "request error" string.
<SPAN class="keyword token">for</SPAN> url <SPAN class="keyword token">in</SPAN> urls<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#iterate through list of URLs</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#download the homepage</SPAN>
response <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#parse the downloaded homepage and grab all text</SPAN>
soup <SPAN class="operator token">=</SPAN> BeautifulSoup<SPAN class="punctuation token">(</SPAN>response<SPAN class="punctuation token">.</SPAN>text<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"lxml"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#find sought after text</SPAN>
item <SPAN class="operator token">=</SPAN> soup<SPAN class="punctuation token">.</SPAN>find<SPAN class="punctuation token">(</SPAN>string<SPAN class="operator token">=</SPAN>re<SPAN class="punctuation token">.</SPAN>compile<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Grab and Go"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> soup<SPAN class="punctuation token">.</SPAN>find<SPAN class="punctuation token">(</SPAN>string<SPAN class="operator token">=</SPAN>re<SPAN class="punctuation token">.</SPAN>compile<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Grab and Go"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="string token">"N/A"</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>item<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#catch any request error </SPAN>
<SPAN class="keyword token">except</SPAN> requests<SPAN class="punctuation token">.</SPAN>exceptions<SPAN class="punctuation token">.</SPAN>RequestException<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"request error"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>How can I identify the URL causing that error? Maybe something like:
<SPAN class="keyword token">except</SPAN> requests<SPAN class="punctuation token">.</SPAN>exceptions<SPAN class="punctuation token">.</SPAN>RequestException<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"request error: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="comment token">#URL that caused error))</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>