[英]Selenium Multiprocessing Help in Python 3.4
我非常想使用Selenium來獲取網站上特定搜索的結果數。 基本上,我想使過程運行得更快。 我有通過迭代搜索詞然后由報紙迭代並將收集的數據輸出為CSV的代碼。 目前,這可以在3年內生成3個搜索字詞x 3個報紙,每個CSV文件能在10分鍾內為我提供9個CSV文件。
我想使用多重處理來同時或至少更快地運行每個搜索和報紙組合。 我嘗試在此處遵循其他示例,但是未能成功實現它們。 下面是我到目前為止的代碼:
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)
如果我在main()函數中添加了一條print語句,它將進行打印,但實際上並沒有發生任何事情。 我將不勝感激。 我遺漏了獲取值的代碼,並將其放入列表中,因為它令人費解,但我知道它可以工作。
在此先感謝您提供的所有幫助! 讓我知道我是否可以提供更多信息。
以撒
編輯:我已經在線尋求更多幫助,意識到我誤解了使用pool.map(fn,list)將列表映射到函數的目的。 我已經更新了代碼,以反映當前仍然無法使用的方法。 我還將初始化值移到了main函數中。
我不認為這可以按照您的方式進行多處理。 因為那里仍然有硒引起的隊列處理(不是隊列模塊)。
原因是...硒只能處理一個窗口,不能同時處理多個窗口或選項卡瀏覽器(window_handle功能的限制)。 這意味着....您的多進程僅處理內存中發送到硒或被硒抓取的數據進程。 通過嘗試在一個腳本文件中處理硒的爬網,將使硒成為瓶頸工藝的源頭。
實現真正的多進程的最佳方法是:
例如:
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)
我可以提供更多解釋,因為這是該過程的主要核心,只有您自己理解,您想在該網絡上做什么。
一種。 設計URL,多進程意味着創建一些進程並與所有CPU內核一起運行,這是實現它的最佳方法...首先是確定輸入過程,在您的情況下,也許是URL目標(您不給我們) ,您要抓取的網站目標)。 但是網站的每個頁面都有不同的網址。 只需收集所有url並將其分為幾個組即可(最佳實踐:您的cpu核心-1)
例如:
import multiprocessing as mp
cpucore=int(mp.cpu_count())-1.
b。 將網址發送到使用您之前進行過的crawl.py處理(通過子流程或其他模塊,例如os.system)。 確保您運行crawl.py max == cpucore。
例如:
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。 繼續您的腳本過程來處理您已經獲得的數據。
用這種方法:
您可以同時打開許多瀏覽器窗口並進行處理,並且由於從網站爬網進行的數據處理比內存中的數據處理要慢,因此該方法至少可以減少數據流的瓶頸。 意味着它比以前的方法要快。
希望有幫助...歡呼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.