Sep-20-2020, 11:35 PM
This is just a fragment of your code; However, I would refactor as follows:
1) use
2) Lines No. 8-13: return url? these lines could be pushed to a function, e.g.
3)
4) you don't need to wrap
5)
6) Use
7) Another option is to use
1) use
logging
instead of printing;2) Lines No. 8-13: return url? these lines could be pushed to a function, e.g.
def get_url(...):
3)
requests.get
-- may lead to an exception (e.g. if url is bad); you probably need to wrap it with try/except
;4) you don't need to wrap
status_code
checking with try/except
(answer to your question); 5)
csv.reader
can raise an exception, if loaded content is not csv-compliant; probably, you need to put try/except here. Anyway, I would put these lines into some function.6) Use
with
when opening files, e.g. with open(os.path.join('html', AddressFile) as out_file:
(in this case you don't need to care about closing the file (out_file.close() isn't needed anymore))7) Another option is to use
pathlib
when you need concatenate paths.