尝试多处理时无法pickle本地对象

2024-06-06 17:15:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着对一个网站进行多处理抓取,在那里我得到我想要从中获取信息的所有节点的列表,然后生成一个池,这样就不用一个一个地并行获取数据了。我的代码如下:

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 multiprocessing

def ResPartido(node):

   ft=node.find_element_by_css_selector('.status').text
   if ft.strip()!='FT': return
   hora=node.find_element_by_css_selector('.time').text
   names=list()
   for nam in node.find_elements_by_xpath(
            './/td[contains(@style,"text-align")]/a[contains(@id,"team")]'):
     name=nam.text
     if '(N)' in name:
        name=name.split('(N)')[0]
     names.append(name)
   score=node.find_element_by_css_selector('.red')

   return [hora,name,score.text]

if __name__ == "__main__":

   browser=webdriver.Chrome()
   SOME CODE
   nodes=browser.find_elements_by_xpath(
        '//tr[contains(@align,"center")]/following-sibling::tr[.//div[contains(@class,"toolimg")]]')
   p = multiprocessing.Pool()

   p.map(ResPartido,nodes)   <---Here is the error
   .......

   >>AttributeError: Can't pickle local object '_createenviron.<locals>.encodekey'

带有错误的python终端的图像

检查文档,它说列表是可选择的对象,在主对象之前声明的函数也是如此,所以我不明白在使用多处理时我做错了什么。在


Tags: textnamefromimportnode列表byif
1条回答
网友
1楼 · 发布于 2024-06-06 17:15:29

根据我所读到的,问题是nodes是一个webdriver对象的列表,这些对象不可序列化。考虑到这一点,我唯一能想到的方法就是下面这些。在

1-不要将整个标记作为节点列表的元素来获取,而是只获取使其与其他元素唯一的标记。在我的示例中,每行都有一个序列号标识符

nodes=browser.find_elements_by_xpath(
    '//tr[contains(@align,"center")]/following-sibling::tr[.//div[contains(@class,"toolimg")]]/@id').get_attribute()

nodes=['1232489','1242356',......]

2-将它与浏览器一起传递给map函数

^{pr2}$

3-在ResPartido函数中,找到具有标识其@id的字符串的唯一行

browser.find_elements_by_xpath('//tr[contains(@id,%s)]' %s node)

有了这个旁路,我还没有测试过,我想我可以得到我想要的东西,而不会遇到可酸洗对象的问题

相关问题 更多 >