在字符串中查找子字符串不返回真实的python

时间:2020-07-12 17:41:23

标签: python python-3.x web-scraping substring

我有这些标签,这些标签是从Web抓取工具返回的,作为我正在制作的CLI应用程序的一部分,我试图查看标签中的子字符串是否包含单词表中的一行。我试图将两个值都手动转换为字符串,但是由于某种原因,什么也没发生,并且在字符串中从未找到子字符串

我尝试使用下面的方法和'in'运算符,但没有成功

这是我使用的方法

for tag in inputs:
    for line in input_wordlist:
        print(tag,line)
        if tag.find(str(line)): # check here if the substring is in the string
            print('YES THIS MATCHES')
            vulns.append(line) #add the vulnerability to the list

这是我正在比较的两个值的示例 左:标签作为字符串 右:我用作子串的单词列表中的那一行

<input id="q-universal-search" type="hidden" value=""/>     hidden

2 个答案:

答案 0 :(得分:0)

您应该使用if substring in line形式的if。像这样

for tag in inputs:
    for line in input_wordlist:
        print(tag,line)
        if line in tag: # check here if the substring is in the string

            print('YES THIS MATCHES')
            vulns.append(line) #add the vulnerability to the list

答案 1 :(得分:0)

看一下print(tag,line)的输出:

<input id="q-universal-search" type="hidden" value=""/>     hidden

line变量似乎有一些空白需要修剪。

尝试if line.strip() in tag:if line.rstrip() in tag:进行匹配。

相关问题