我正在编写一个工具,该工具可获取多个HTML文件并将其作为文本进行处理:
for url in url_list:
url_response = requests.get(url)
text = url_response.text
# Process text here (put in database, search, etc)
问题是,这非常慢。如果只需要一个简单的响应,就可以使用grequest,但是由于我需要获取HTML文件的内容,因此这似乎不是一种选择。我该如何固定呢?
谢谢!
import requests
from multiprocessing import Pool
def process_html(url):
url_response = requests.get(url)
text = url_response.text
print(text[:500])
print('-' * 30)
urls = [
'http://www.apple.com',
'http://www.yahoo.com',
'http://www.google.com',
'http://www.apple.com',
'http://www.yahoo.com',
'http://www.google.com',
'http://www.apple.com',
'http://www.yahoo.com',
'http://www.google.com',
]
with Pool(None) as p: #None => uses cpu.count()
p.map(process_html, urls) #This blocks until all return values from process_html() have been collected.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句