如何同时下载多个链接?我下面的脚本可以工作,但一次只能下载一个,而且速度非常慢。我不知道如何在脚本中合并多线程。
Python脚本:
from BeautifulSoup import BeautifulSoup
import lxml.html as html
import urlparse
import os, sys
import urllib2
import re
print ("downloading and parsing Bibles...")
root = html.parse(open('links.html'))
for link in root.findall('//a'):
url = link.get('href')
name = urlparse.urlparse(url).path.split('/')[-1]
dirname = urlparse.urlparse(url).path.split('.')[-1]
f = urllib2.urlopen(url)
s = f.read()
if (os.path.isdir(dirname) == 0):
os.mkdir(dirname)
soup = BeautifulSoup(s)
articleTag = soup.html.body.article
converted = str(articleTag)
full_path = os.path.join(dirname, name)
open(full_path, 'w').write(converted)
print(name)
名为links.html
的HTML文件:
<a href="http://www.youversion.com/bible/gen.1.nmv-fas">http://www.youversion.com/bible/gen.1.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.2.nmv-fas">http://www.youversion.com/bible/gen.2.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.3.nmv-fas">http://www.youversion.com/bible/gen.3.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.4.nmv-fas">http://www.youversion.com/bible/gen.4.nmv-fas</a>
我使用
multiprocessing
来并行化事物——出于某种原因,我更喜欢它而不是threading
2017年,还有其他一些选择,比如asyncio和ThreadPoolExecutor。
以下是ThreadPoolExecutor的一个示例(包含在Python futures中)
函数的作用是:将任务提交到队列中。(队列管理为您完成)
您可以设置max_workers,实际上是CPU核心数的几倍,然后根据上下文切换开销进行一些测试,看看faw的性能如何提高。
更多信息: https://docs.python.org/3/library/concurrent.futures.html
在我看来,这就像是消费者-生产者问题-参见维基百科
你可以用
相关问题 更多 >
编程相关推荐