Python 多进程selenium爬取MAC地址

from multiprocessing import Pool
import os, time, random
from selenium import webdriver
from scrapy.selector import Selector
from selenium import webdriver
from time import strftime,gmtime
from selenium.webdriver.common.keys import Keys


count=0
macs = open('D:\\Python log\\data\\pa_zte_mac1w_2018101458.txt')
browser=webdriver.Chrome(executable_path='D:\Program Files\python36\chromedriver.exe')
browser.get("https://mac.51240.com/")


def long_time_task(mac):
    print('Run task %s (%s)...' % (mac, os.getpid()))
    start = time.time()
    # time.sleep(random.random() * 3)
    mac = mac.strip()
    print(mac)
    browser.find_element_by_xpath('//*[@id="zdssname"]').clear()
    browser.find_element_by_xpath('//*[@id="zdssname"]').send_keys(str(mac))
    browser.find_element_by_xpath('//*[@id="main_content"]/div[1]/input[2]').click()
    time.sleep(4)
    factory=browser.find_element_by_css_selector('#main_content > table > tbody > tr > td > table > tbody > tr:nth-child(2) > td:nth-child(2)').text
    time.sleep(0.2)
    print(factory)
    print(browser.current_url)
    print(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
    logfile = open('D:\\Python log\\data\\factory_pa_zte_mac1w_2018101458.txt', 'a')
    logfile.write(mac + " ,")
    logfile.write(factory + '\n')
    logfile.close()
    time.sleep(0.2)
    end = time.time()
    print('Task %s runs %0.2f seconds.' % (mac, (end - start)))


if __name__=='__main__':
    print('Parent process %s.' % os.getpid())
    p = Pool(processes=2)
    for mac in macs:
        p.apply_async(long_time_task, args=(mac,))
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')
    browser.quit()
阅读更多
文章标签: Python 多进程
个人分类: python
想对作者说点什么? 我来说一句

爬取图片,并且保存mysql

2018年05月15日 2KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭