为什么我的Python脚本要在多个网页上查找文本,从终端运行要花这么长时间?
我使用下面的Python搜索多个网页中的特定文本,然后打印找到的文本以及与文本相关的任何HREF。然而,这个脚本似乎要花很长时间才能从终端上运行(扫描3个网站需要一个多小时,但没有结果)。有人能告诉我我做错了什么吗为什么我的Python脚本要在多个网页上查找文本,从终端运行要花这么长时间?,python,macos,web-scraping,beautifulsoup,terminal,Python,Macos,Web Scraping,Beautifulsoup,Terminal,我使用下面的Python搜索多个网页中的特定文本,然后打印找到的文本以及与文本相关的任何HREF。然而,这个脚本似乎要花很长时间才能从终端上运行(扫描3个网站需要一个多小时,但没有结果)。有人能告诉我我做错了什么吗 import requests from bs4 import BeautifulSoup #Sites to scan url_list = ["somesite1","somesite2","somesite3"]
import requests
from bs4 import BeautifulSoup
#Sites to scan
url_list = ["somesite1","somesite2","somesite3"]
#Keywords to search
the_word = 'Some Text'
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 the_word.lower() in text)
count = len(words)
words_list = [ ele.strip() for ele in words ]
for word in words:
total_words.append(word.strip())
print('\nUrl: {}\ncontains {} of word: {}'.format(url, count, the_word))
print(words_list)
#print(total_words)
total_count = len(total_words)
你能分享这个网站的URL和你想找到的文本吗?我有一个网站列表,我正在为学校做一个案例研究。尝试查看哪些网站有“不出售我的个人信息”链接,哪些没有。示例站点:apple.com、cnn.com等。