如何在使用python进行多处理时使用特定于池的函数?

时间:2016-11-13 23:03:54

标签: python function selenium multiprocessing

我使用selenium和多处理来生成四个不同的网站,我想运行特定于驱动程序生成的网站的功能。

这与我目前的代码类似:

from multiprocessing import Pool
from selenium import webdriver

def gh(hosts):
    driver = webdriver.Chrome(executable_path='./chromedriver')
    driver.get(hosts)
    html_source = driver.page_source
        if 'ryan' in html_source:
            print 'ryan'
            doSomethingForRyan()
        elif 'austin' in html_source:
            print 'austin'
            doSomethingForAustin()
        elif 'travis' in html_source:
            print 'travis'
            doSomethingForTravis()
        elif 'levi' in html_source:
            print 'levi'
            doSomethingForLevi()
        else:
            print '--NONE--'

if __name__ == '__main__':
    p = Pool(4)

    hosts = ["http://ryan.com", "https://www.austin.com",    "http://levi.com", "http://travis.com"]
    p.map(gh, hosts)

我得到的结果如下: 奥斯汀 奥斯汀 瑞安 奥斯汀

1 个答案:

答案 0 :(得分:0)

编辑 - 已解决

不是从driver.page_source读取,而是从driver.current_url读取,以确保我可以运行特定于网站的功能。

if 'ryan' in driver.current_url:
    print 'ryan'
    doStuff()