在使用Python的URL列表中搜索特定单词列表
我正在尝试确定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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在for循环中,您将CCI_Words投入到下行中的列表中,因此您的程序第二次通过循环进行迭代后会丢弃错误,并尝试在CCI_Words上调用Lower()。
如果您希望列表中的任何单词返回true,您似乎使这个问题很复杂,可以做这样的事情。
如果您想检查列表中的所有单词,可以尝试此方法。
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.
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.
If you want to check for all the words in the list, you could try this approach.