Skip to main content
 首页 » 编程设计

python之python 3中的简单多线程

2025年02月15日38freeliver54

我创建了一个简单的 python 程序,它可以抓取我最喜欢的食谱网站并从主站点返回各个食谱 URL。虽然这是一个相对快速和简单的过程,但我已经尝试将其扩展以抓取站点内的多个网页。当我这样做时,从整个网站上抓取所有食谱 URL 大约需要 45 秒。我希望这个过程快得多,所以我尝试在我的程序中实现线程。

我意识到这里有问题,因为每个线程一遍又一遍地返回整个 URL 线程,而不是“拆分”工作。有没有人对如何更好地实现线程有任何建议?我在下面包括了我的工作。使用 Python 3。

from bs4 import BeautifulSoup 
import urllib.request 
from urllib.request import urlopen 
from datetime import datetime 
import threading 
 
from datetime import datetime 
 
startTime = datetime.now() 
 
quote_page='http://thepioneerwoman.com/cooking_cat/all-pw-recipes/' 
page = urllib.request.urlopen(quote_page) 
soup = BeautifulSoup(page, 'html.parser') 
 
all_recipe_links = [] 
 
#get all recipe links on current page 
def get_recipe_links(): 
    for link in soup.find_all('a', attrs={'post-card-permalink'}): 
        if link.has_attr('href'): 
            if 'cooking/' in link.attrs['href']: 
                all_recipe_links.append(link.attrs['href']) 
 
    print(datetime.now() - startTime) 
    return all_recipe_links 
 
 
def worker(): 
    """thread worker function""" 
    print(get_recipe_links()) 
    return 
 
threads = [] 
for i in range(5): 
    t = threading.Thread(target=worker) 
    threads.append(t) 
    t.start() 

请您参考如下方法:

通过让所有工作人员都处理来自单个列表的数据,而不是让他们都单独运行整个方法,我能够将工作分配给工作人员。以下是我更改的部分。不再需要 get_recipe_links 方法,因为它的任务已移至其他方法。

all_recipe_links = [] 
links_to_process = [] 
 
def worker(): 
    """thread worker function""" 
    while(len(links_to_process) > 0): 
        link = links_to_process.pop() 
        if link.has_attr('href'): 
                if 'cooking/' in link.attrs['href']: 
                    all_recipe_links.append(link.attrs['href'])         
 
threads = [] 
links_to_process = soup.find_all('a', attrs={'post-card-permalink'}) 
for i in range(5): 
    t = threading.Thread(target=worker) 
    threads.append(t) 
    t.start() 
 
while len(links_to_process)>0: 
    continue 
print(all_recipe_links) 

我多次运行新方法,平均运行时间为 0.02 秒。