问题描述
伙计们,我想知道是否有一种方法可以让我的机器人在1次尝试继续使用for循环后跳过无效的url,但continue似乎不起作用
def check_valid(stripped_results):全局vstripped_resultsvstripped_results = []对于stripped_results中的标记:conn = request.head("https://" +标记)conn2 = request.head("http://" +标记)status_code = conn.status_codewebsite_is_up = status_code == 200如果website_is_up:vstripped_results.append(标签)别的:继续
剥离后的结果是一系列未知数量的域和子域,这就是为什么我使用'https://'部分的原因,而且我甚至不确定自己的if语句是否有效.任何帮助将不胜感激,我不想再因发送这么多无效域而受到不和谐的限制.:(
这很容易.为了检查URL的有效性,存在一个python库,即 Validators
.该库可用于验证任何URL是否存在.让我们逐步进行吧.
首先,这是验证程序的文档链接:
在该问题的链接中,它给出了 True
,如果为假,则将为您提供错误消息.
您可以使用以下语法对其进行验证:
validators.url('在此处添加您的URL变量')
请记住,这会提供布尔值,因此可以这样编写代码.
所以您可以通过这种方式使用它...
我不会在您的代码中实现它,因为我希望您自己尝试一次.如果您无法执行此操作,我会为您提供帮助.
谢谢!:)
Hey guys I was wondering if there was a way to make my bot skip invalid urls after 1 try to continue with the for loop but continue doesn't seem to work
def check_valid(stripped_results):
global vstripped_results
vstripped_results = []
for tag in stripped_results:
conn = requests.head("https://" + tag)
conn2 = requests.head("http://" + tag)
status_code = conn.status_code
website_is_up = status_code == 200
if website_is_up:
vstripped_results.append(tag)
else:
continue
stripped results is an array of an unknown amount of domains and Subdomains which is why I have the 'https://' part and tbh I'm not even sure whether my if statement is effective or not.Any help would be greatly appreciated I don't want to get rate limited by discord anymore from sending so many invalid domains through. :(
This is easy. To check the validity of a URL there exist a python library, namely Validators
. This library can be used to validate any URL for if it exist or not. Let's take it step by step.
Firstly,Here is the documentation link for validators:https://validators.readthedocs.io/en/latest/
How do you validate a link using validators
?
It is simple. Let's work on command line for a moment.
This image shows it. This module gives out boolean result on if it is a valid link or not.
Here for the link of this question it gave out True
and when it would be false then it would give you the error.
You can validate it using this syntax:
validators.url('Add your URL variable here')
Remember that this gives boolean value so code for it that way.
So you can use it this way...
I wouldn't be implementing it in your code as I want you to try it yourself once. I would help you with this if you are unable to do it.
Thank You! :)
这篇关于如何使我的机器人跳过不存在的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!