Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查url在python 3中是否有特定字符串_Python_Python 3.x - Fatal编程技术网

检查url在python 3中是否有特定字符串

检查url在python 3中是否有特定字符串,python,python-3.x,Python,Python 3.x,我是python新手,我无法理解这一点 在这段代码中,我需要检查url是否有http for link in links: if "http" in link.get("href"): print("<a href='%s'>%s</a>" % (link.get("href"), link.text)) 对于链接中的链接: 如果link.get(“href”)中的“http”: 打印(“%”(link.get(“href”)、link.text

我是python新手,我无法理解这一点

在这段代码中,我需要检查url是否有http

for link in links:
    if "http" in link.get("href"):
        print("<a href='%s'>%s</a>" % (link.get("href"), link.text))
对于链接中的链接:
如果link.get(“href”)中的“http”:
打印(“%”(link.get(“href”)、link.text))
运行时,我遇到以下错误:

TypeError:类型为“NoneType”的参数不可编辑

我怎样才能解决这个问题?
提前感谢您的帮助。

您可以尝试使用
字符串。查找

但您的问题似乎是
link.get(“href”)
没有返回任何内容


你的链接可能没有
“href”

我不得不猜测一下你的上下文到底是什么。但这可能对你有帮助

您可以通过“IfVarIsNone:”并继续循环来检查某些内容是否为None

但我的建议是从基础教程开始,而不是直接跳到一些具体的任务中。。。这对您来说可能更容易:)

从bs4导入美化组
进口稀土
网站=“插入HTML代码”
soup=BeautifulSoup(网站“html.parser”)
p=重新编译(“https://”)
soup=BeautifulSoup(网站“html.parser”)
soup\u links=soup.find\u all(“a”)
打印(len(汤_链接))
计数器=0
对于“汤”链接中的链接:

如果链接为无:#您可以共享链接中的内容吗?请检查“链接”。也许它应该是一个列表谢谢你的回复<代码>链接=汤。查找所有(“a”)
请提供堆栈跟踪。您所说的“堆栈跟踪”是什么意思。。请原谅我,因为我是个初学者,非常感谢。你能给我提供如何使用find的代码吗?如果你提到的某些元素没有,怎么处理呢?非常感谢。你的猜测是完美的,这正是我一直在寻找的,但在测试时,我得到了所有的链接。。我只需要它的http链接string@YasserKhalil我更新了我的代码片段,并使用正则表达式而不是字符串。查找。。。希望这对你也有用!我的例子现在效果很好,很好。非常感谢你的帮助,不客气!祝你晚上愉快!很高兴我能帮忙!
from bs4 import BeautifulSoup
import re

website = """#INSERT_HTML_CODE""" 
soup = BeautifulSoup(website, 'html.parser')

p = re.compile("https://")
soup = BeautifulSoup(website, 'html.parser')

soup_links = soup.find_all("a")
print(len(soup_links))

counter = 0

for link in soup_links:
    if link is None: # <---- Handle None value with continuing the loop
        continue

    if p.match(link.get("href", "")) is not None: # <--- Handle link element, if https is in href String.
        # If href is not existing. .get() returns "" and nothing is broken
        print("HTTPS found")
        print("<a href='%s'>%s</a>" % (link.get("href"), link.string) )
        print("")
        counter = counter + 1

print(counter)