我在我头上试图用硒来获得结果在网站上的特定搜索的次数。基本上,我想使这一进程的运行速度。我有一个由迭代搜索条件,然后通过报纸工作和收集到的数据输出到CSV代码。目前,这个运行产生3个搜索字词×3份报纸在3年内给我9个CSV的每CSV约10分钟。
我想用多同时或至少更快运行每个搜索和报纸的组合。我试着遵循这里的其他例子,但一直没能成功地实现它们。下面是我到目前为止的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os
import pandas as pd
from multiprocessing import Pool
def websitesearch(search):
try:
start = list_of_inputs[0]
end = list_of_inputs[1]
newsabbv=list_of_inputs[2]
directory=list_of_inputs[3]
os.chdir(directory)
if search == broad:
specification = "broad"
relPapers = newsabbv
elif search == narrow:
specification = "narrow"
relPapers = newsabbv
elif search == general:
specification = "allarticles"
relPapers = newsabbv
else:
for newspapers in relPapers:
...rest of code here that gets the data and puts it in a list named all_Data...
browser.close()
df = pd.DataFrame(all_Data)
df.to_csv(filename, index=False)
except:
print('error with item')
if __name__ == '__main__':
...Initializing values and things like that go here. This helps with the setup for search...
#These are things that go into the function
start = ["January",2015]
end = ["August",2017]
directory = "STUFF GOES HERE"
newsabbv = all_news_abbv
search_list = [narrow, broad, general]
list_of_inputs = [start,end,newsabbv,directory]
pool = Pool(processes=4)
for search in search_list:
pool.map(websitesearch, search_list)
print(list_of_inputs)
如果我在一个print语句在main()函数中添加,将打印,但没有真正结束意外事件发生。我会很感激的任何和所有帮助。我离开了那得到的值,并将其放入自曲列表中的代码,但我知道它的工作原理。
预先感谢任何及所有的帮助!让我知道如果有我可以提供更多的信息。
艾萨克
编辑:我看着更多的帮助在线和意识到,我误解使用pool.map(FN,列表)映射列表功能的目的。我已经更新了我的代码,以反映仍然没有工作我目前的做法。我也感动初始化值代入的主要功能。
我不认为它可以用自己的方式进行多处理。因为它仍然有排队过程中出现(不排队模块)由硒引起的。
原因是...硒只能处理一个窗口,在同一时间(的WINDOW_HANDLE功能限制),不能处理多个窗口或标签浏览器。这是手段....你多过程中只处理数据过程中的内存发送到硒或硒抓取。通过尝试处理硒的爬在一个脚本文件,将使硒作为瓶颈过程的源代码。
使真正的多进程的最好办法是:
e.g:
import -> all modules that you need to run selenium
import sys
url = sys.argv[1] #you will catch the url
driver = ......#open browser
driver.get(url)
#just continue the script base on your method
print(--the result that you want--)
sys.exit(0)
我可以给更多的解释,因为这不仅是过程的主要核心,并且希望该网站上做什么,你的理解。
一种。 devide的URL,多工艺手段使一些过程,并与所有的CPU内核,使其...它开始由devide输入过程的最佳途径,你的情况一起运行它也许是URL目标(你不给我们,你想要的网站目标抓取)。但该网站的每一个页面都有不同的URL。只是收集所有的URL,并将其devide若干个组(最佳实践:你的CPU核心 - 1)
e.g:
import multiprocessing as mp
cpucore=int(mp.cpu_count())-1.
湾将该网址发送给处理与已你之前所做的crawl.py(由子,或其他模块E,G:使用os.system)。请确保您运行crawl.py最大==的cpucore。
e.g:
crawler = r'YOUR FILE DIRECTORY\crawler.py'
def devideurl():
global url1, url2, url3, url4
make script that result:
urls1 = groups or list of url
urls2 = groups or list of url
urls3 = groups or list of url
urls4 = groups or list of url
def target1():
for url in url1:
t1 = subprocess.Popen(['python', crawler, url], stdout = PIPE)
#continue the script, base on your need...
#do you see the combination between python crawler and url?
#the cmd command will be: python crawler.py "value", the "value" is captured by sys.argv[1] command in crawler.py
def target2():
for url in url2:
t1 = subprocess.Popen(['python', crawler, url], stdout = PIPE)
#continue the script, base on your need...
def target3():
for url in url1:
t1 = subprocess.Popen(['python', crawler, url], stdout = PIPE)
#continue the script, base on your need...
def target4():
for url in url2:
t1 = subprocess.Popen(['python', crawler, url], stdout = PIPE)
#continue the script, base on your need...
cpucore = int(mp.cpu_count())-1
pool = Pool(processes="max is the value of cpucore")
for search in search_list:
pool.map(target1, devideurl)
pool.map(target2, devideurl)
pool.map(target3, devideurl)
pool.map(target4, devideurl)
#you can make it, more, depend on your cpu core
C。拿到打印的结果主脚本的记忆
d。连续你的脚本程序来处理,你已经获得了数据。
用这种方法:
你可以打开许多浏览器窗口,并在同一时间处理它,而且由于数据处理,从网站上检索比内存中的数据处理速度较慢,这种方法至少减少对数据流的瓶颈。意味着它比以前你的方法来得更快。
希望有帮助...干杯