在使用Python的URL列表中搜索特定单词列表

发布于 2025-02-12 08:36:10 字数 780 浏览 1 评论 0原文

我正在尝试确定URL列表是否包含特定单词。以下是我的代码:


url_list = ['website1.com', 'website2.com']

cci_words = ['Risk Management', 'Labor', 'Migrant Workers']

total_words = []
for url in url_list:
    r = requests.get(url, allow_redirects=False)
    soup = BeautifulSoup(r.content.lower(), 'lxml')
    words = soup.find_all(text=lambda text: text and cci_words.lower() in text)
    count = len(words)
    cci_words = [ ele.strip() for ele in words ]
    for word in words:
        total_words.append(word.strip())

    print('\nUrl: {}\ncontains {} of word: {}'.format(url, count, cci_words))
    print(cci_words)

#print(total_words)
total_count = len(total_words)

但是我一直遇到此错误:attributeError:'list'对象没有属性“降低”

任何想法我该怎么办?

I'm trying to determine whether or not a list of URLs contain specific words. Below is my code:


url_list = ['website1.com', 'website2.com']

cci_words = ['Risk Management', 'Labor', 'Migrant Workers']

total_words = []
for url in url_list:
    r = requests.get(url, allow_redirects=False)
    soup = BeautifulSoup(r.content.lower(), 'lxml')
    words = soup.find_all(text=lambda text: text and cci_words.lower() in text)
    count = len(words)
    cci_words = [ ele.strip() for ele in words ]
    for word in words:
        total_words.append(word.strip())

    print('\nUrl: {}\ncontains {} of word: {}'.format(url, count, cci_words))
    print(cci_words)

#print(total_words)
total_count = len(total_words)

But I keep getting this error: AttributeError: 'list' object has no attribute 'lower'

Any ideas what should I do??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾旎 2025-02-19 08:36:10

在for循环中,您将CCI_Words投入到下行中的列表中,因此您的程序第二次通过循环进行迭代后会丢弃错误,并尝试在CCI_Words上调用Lower()。

cci_words = [ ele.strip() for ele in words ]

如果您希望列表中的任何单词返回true,您似乎使这个问题很复杂,可以做这样的事情。

def words_in_url_list(words: List[str], url_list: List[str]) -> bool:
    count = 0
    for word in words:
        word = word.lower()
        [count += 1 for url in url_list if word in url]
    return count > 0

如果您想检查列表中的所有单词,可以尝试此方法。

def all_words_in_url_list(words: List[str], url_list: List[str]) -> bool:
    comparison: set[str] = set()
    for word in words:
        word = word.lower()
        [comparison.add(word) for url in url_list if word in url]
    return len(words) == len(comparison)
        

In your for loop you cast cci_words to a list in the below line, so your program is throwing an error after it iterates through the loop a second time and tries to call lower() on cci_words.

cci_words = [ ele.strip() for ele in words ]

You seem to be making this probem quite complex where you could do something like this if you want any word in the list being present to return True.

def words_in_url_list(words: List[str], url_list: List[str]) -> bool:
    count = 0
    for word in words:
        word = word.lower()
        [count += 1 for url in url_list if word in url]
    return count > 0

If you want to check for all the words in the list, you could try this approach.

def all_words_in_url_list(words: List[str], url_list: List[str]) -> bool:
    comparison: set[str] = set()
    for word in words:
        word = word.lower()
        [comparison.add(word) for url in url_list if word in url]
    return len(words) == len(comparison)
        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文