There is a miss conception in your function. One time the function returns on success the link, if the function fails, it returns
In addition you can work without regex, which is often easier:
False
. 2 days before I had in the German forum this discussion. The problem was, that the guy was using an API for a HX711, which was made by the community. The API has a read function, which is used internally to get the bits/bytes. Sometimes it fails. The problem is, that the internal read function sometimes return the Value and if no success, it returns False, which is a SubType of Integer. When the outside caller, calls for example the function to calculate the mean, there is one problem. Sometimes he calculates the mean value with False, which is 0, which will return a wrong mean. If you have more code working together, you'll have similar problems. Your function should return only True
or False
. If you want to return the link also, you should return (True, link)
or (False, link)
.In addition you can work without regex, which is often easier:
import operator import requests def send_get_request(link, search_for): """ send a get request to url, return True if all stings are contained in html.text. """ try: html = requests.get(link) except requests.exceptions.RequestException as e: print("Error: {}" . format(e)) # maybe you want to handle the error on # caller side and not here. # depends on the logic and flow in your program return False # don't continue here text = html.text.lower() if all(operator.contains(text, keyword.lower()) for keyword in search_for): return True else: return FalseInstead of using the keyword
in
(is an operator; contains), I use the function operator.contains
together with all
as a generator expression.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!