I have a function that will return different values depending on an if statement in the function. The returned value will be added to an existing list. However, in the function there is a case where I want the function to do nothing, i.e., the existing list will keep the same as if no function were called. Currently what I'm doing is the following, but I feel this looks messy and wondering if there is a better way to do:
import requests
def myfun(url):
response=requests.get(url)
code=response.status_code
if code==503:
time.sleep(3*random.random())
value=None
else:
html=response.content()
value=html['some tag']
return (value,code)
lists=[]
for url in [my url pool]:
(value,code)=myfun(url)
lists.append(value)
if code==503:
lists.pop()
So basically what I do is to continuously visit some webpage, read some values then put the values in a growing list. However, sometime I get blocked (where Error Code 503 is given), then I want my code to sleep for a while then keep moving forward. In the case where I get 503 error code, nothing will be returned so the growing list will be the same. But I don't know how to specify this case when I call the function, so I just set the returned values to None (or could be any value), then add them to the list but remove them from the list right after that. (So I use append() then pop() if the code is 503)
Instead of appending the value, then checking for a 503 and removing the value...
lists.append(value)
if code==503:
lists.pop()
Check that the response is not 503 and then append the value.
if code!=503:
lists.append(value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With