比较三个文本列表以查看匹配的单词

时间:2019-03-30 15:46:01

标签: python

您好,我编写了一个函数来读取和比较三个句子列表中的单词,如果任何个单词匹配,该函数将返回其他文本False,基本上是一个列表来自selenium的网络元素并进行检查 如果文本与keywords的列表中的任何一个匹配,我要做的就是修改它,如果1或3或更多,它在检查后返回链接,即如果只有两个单词匹配则返回False。(此函数将如果任何单词匹配且其中一个关键字匹配链接,则返回链接) 我想如果(1,3,4,5 ...)个单词匹配并且一个关键字匹配链接(仅0,2返回False),此函数将返回链接 linkstexts的长度相等。

from selenium import webdriver
d = webdriver.Chrome(executable_path=r"C:\Users\test\PycharmProjects\chromedriver")
sentence = "hello world from python"
url_keywords = [".com",".edu"]
d.get("https://google.com/search?q={}".format(sentence))
y=d.find_elements_by_xpath("//a[@href]")
a=check(y,url_keywords)
li=[]
if a:
    check(y)
else:
    pass

def check(y,url_keywords):
    links = [i.get('href') for i in y]
    texts = [i.text_content() for i in y]
    for i, link in enumerate(links):
        for keyword in url_keywords:
            if keyword in link:
                for word in sentence.lower().split():
                    if word in texts[i].lower():
                        return link

    return False

如果有更简便的方法,请提出建议

2 个答案:

答案 0 :(得分:0)

在没有源数据的情况下,最简单的处理方法如下:

[i for i in sent1.lower().split() for j in sent2.lower().split() for k in sent3.lower().split()
    if i == j == k]

答案 1 :(得分:0)

from selenium import webdriver

# Use descriptive names for variables, not single letters.
driver = webdriver.Chrome(executable_path=r"C:\Users\test\PycharmProjects\chromedriver")

# Use UPPERCASE for constants
SENTENCE = "hello world from python"
URL_KEYWORDS = [".com",".edu"]

driver.get("https://google.com/search?q={}".format(sentence))
elements  = driver.find_elements_by_xpath("//a[@href]")
result = check(elements, url_keywords)


def check(elements, url_keywords):
    links = [i.get('href') for i in elements]
    texts = [i.text_content() for i in elements]

    # Use zip to avoid so much nesting! Also means you can drop the index variable "i"
    search_space = zip(links, texts)

    for link, text in search_space:
        #Let's keep track
        number_of_matches = 0  
        for keyword in url_keywords:
            # Create a separate function, again to avoid so much nesting! (see "Zen of Python")
            match = is_match(keyword, link, text)
            #If match is true int(match) will be 1, otherwise 0
            number_of_matches += int(match)
        if has_correct_number_of_matches(number_of_matches):
            return link
        else:
            return False

def normalise(string):
    """
    There is often quite a bit that we want to do to normalise strings. And you might want to extend this later. For this reason, I again make a new function, and also add in the "strip" method for good measure and as an example of extending the normalisation behaviour.
    """
    return string.lower().strip()

def is_match(keyword, link, text):
    if keyword in link:
        for word in normalise(sentence).split():
            if word in normalise(text):
                return True
        else:
            return False
     else:
        return False

def has_correct_number_of_matches(number_of_matches):
    """Now that this function is isolated, you can define it however you want!
    """
    return number_of_matches not in (0, 2)