'how to check if a link of an input belongs to a specific site in python?
I have a function that will check a URL input, however, there are only two options, which are Amazon and Aliexpress, so I'm trying to check if the link entered in the input belongs to one of the two sites, but I'm not getting it
def url_check(values):
amazon_url = 'https://www.amazon.com'
aliexpress_url = 'https://best.aliexpress.com/'
if amazon_url not in values['url_input']:
sg.popup_error('URL Error','The URL does not belong to the specified site!')
raise ValueError('The URL does not belong to the specified site!')
if not aliexpress_url in values['url_input']:
sg.popup_error('URL Error','The URL does not belong to the specified site!')
raise ValueError('The URL does not belong to the specified site!')
Note: I can't use == instead of in because the input will specify the link to a product from one of the sites, not the home url of the site.
Solution 1:[1]
I did not fully understand what you're asking or what's the problem, but you are always throwing errors if the provided site does not belong to amazon. You should cycle over all the sites that you want to check and raise an exception only if there is no match
def url_check(values):
valid_urls = ['https://www.amazon.com', 'https://best.aliexpress.com/']
for url in valid_urls:
if url in values['url_input']:
return # or do whatever
sg.popup_error('URL Error','The URL does not belong to the specified site!')
raise ValueError('The URL does not belong to the specified site!')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | FedericoCozziVM |
