【发布时间】:2023-05-03 19:16:01
【问题描述】:
我正在使用 Selenium 和 Webdriver 进行 Crawl 项目。由于我需要在大时抓取该数据,因此我想将其拆分为 2 threads 并同时运行。但是当我同时启动2个Webdrivers时,我的代码无法识别哪个driver属于哪个thread来填写信息。
- 这是我设置的代码 2
threads运行main函数:
if __name__ == '__main__':
data = load_data(INPUT_DIR)
t1_data = data[:250]
t2_data = data[250:]
try:
_thread.start_new_thread(main, (t1_data, LOG_FILE_T1))
_thread.start_new_thread(main, (t2_data, LOG_FILE_T2))
except:
print ("Error: unable to start thread")
while 1:
pass
- 这段代码我启动
Webdriver:
def start_driver():
global driver
options = Options()
options.add_argument("--disable-notification")
options.add_argument("--disable-infobars")
options.add_argument("--mute-audio")
#options.add_argument("headless")
driver = webdriver.Chrome(options=options)
- 两个
Webdrivers启动后填写用户名/密码 facebook.com 上的信息
def login(email, password):
""" Logging into our own profile """
try:
driver.get('https://mbasic.facebook.com')
time.sleep(DELAY_TIME)
driver.find_element_by_name('email').send_keys(email)
driver.find_element_by_name('pass').send_keys(password)
driver.find_element_by_name('login').click()
# deal with "Not Now" button if it show off at first time
not_now_button = driver.find_element_by_xpath("//a")
if not_now_button.size != 0:
not_now_button.click()
except Exception as e:
print('Error in Login')
print(e)
exit()
在send_keys这一步,两个threads在同一个文本框中填写1Webdriver。
如何更改我的代码 2 threads 可以看到不同的 Webdrive 并填写信息?
【问题讨论】:
标签: python multithreading selenium selenium-webdriver web-crawler