[英]How to use Multiprocessing with Selenium in Python
我正在嘗試在python中使用硒處理多重處理。 我的代碼如下:
from selenium import webdriver
from multiprocessing import Pool
import xlwings as xw
driver = webdriver.Chrome('chromedriver.exe')
driver.get("https://example.com")
wb = xw.Book('my_file.xlsm')
sht = wb.sheets["Sheet1"]
final_list = []
search = driver.find_element_by_id("ContentPlaceHolder1_txtByName")
for item in search:
z = item.find_element_by_class_name("valuetext")
info = z.find_element_by_tag_name("span")
final_list.append(info.text)
def automate(num):
col = num
list_item = final_list[num]
sht.range(1, col).value = each
if __name__ == '__main__':
p = Pool(processes=4)
data = p.map(automate,range(1,20))
我遇到的問題是網頁的這四個過程中的每個過程都被重新打開了,我不明白為什么。 如果p.map
僅針對automate
函數,那么為什么其余代碼會在每個進程中運行?
我仍然對多處理還不陌生,所以不確定這是否是它的工作方式嗎?是否有另一種方法可以確保進程僅針對函數本身,還是可以使用線程?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.