多处理新手!请帮忙。
所有库都已导入,get_links方法有效,我已经在一个案例中对其进行了测试。尝试使该方法针对指定给并行进程的多个URL运行,以使其更快。没有多处理,我的运行时间是10个小时+
编辑2:
在MCVE上尽了最大努力
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from multiprocessing import Pool
options = Options()
options.headless = True
options.binary_location = 'C:\\Users\\Liam\\AppData\\Local\\Google\\Chrome SxS\\Application\\Chrome.exe'
options.add_argument('--blink-settings=imagesEnabled=false')
options.add_argument('--no-sandbox')
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
subsubarea_urls = []
with open('subsubarea_urls.txt') as f:
for item in f:
item = item.strip()
subsubarea_urls.append(item)
test_urls = subsubarea_urls[:3]
def get_links(url):
driver = webdriver.Chrome('....\Chromedriver', chrome_options=options)
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')
link = soup.find(class_ = 'listings__all')
if link is not None:
link = "example.com" + link.find('a')['href']
driver.close()
return link
def main():
how_many = 3
p = Pool(processes = how_many)
data = p.map(get_links, test_urls)
p.close()
with open('test_urls.txt', 'w') as f:
f.write(str(data))
if __name__ == '__main__':
main()
答案 0 :(得分:0)
出乎意料的是,问题与代码无关。 python中的多处理似乎并不像Windows GUI那样,Pool调用的子进程没有std流。 该代码需要在IDLE python -m idlelib.idle中执行(要打开IDLE)
请参阅Terry Jan Reedy的答案here