嘿,大家都相信你很好,我正在嘗試使用Multiprocessing和Selenium并行運行多個 Instagram 帳戶,我將如何在創建的每個行程中登錄不同的帳戶。
我嘗試使用帶有 while 回圈的 json 檔案,但是我沒有取得太大進展。
我會很感激我能得到的任何幫助。
import time
import json
from selenium import webdriver
from multiprocessing import Pool
f = open('accounts.json',)
datas = json.load(f)
def get_data(url):
Options = webdriver.ChromeOptions()
mobile_emulation = {
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/101.0.4951.64 Mobile Safari/535.19" }
Options.add_experimental_option("mobileEmulation", mobile_emulation)
Options.add_argument("--log-level=3")
bot = webdriver.Chrome(options=Options, executable_path="chromedriver.exe")
bot.set_window_size(500, 768)
bot.get(url=url)
time.sleep(10)
# Login section==========================
print('Logging in...')
bot.find_element_by_xpath(
'//*[@id="react-root"]/section/main/article/div/div/div/div[3]/button[1]').click()
time.sleep(5)
username_field = bot.find_element_by_xpath(
'//*[@id="loginForm"]/div[1]/div[3]/div/label/input')
username_field.send_keys(data["username"])
time.sleep(5)
password_field = bot.find_element_by_xpath(
'//*[@id="loginForm"]/div[1]/div[4]/div/label/input')
password_field.send_keys(data["password"])
time.sleep(5)
bot.find_element_by_xpath(
'//*[@id="loginForm"]/div[1]/div[6]/button').click()
time.sleep(6)
bot.close()
bot.quit()
if __name__ == '__main__':
process_count = int(input("Enter the number of processes: "))
url = "https://www.instagram.com/"
urls_list = [url] * process_count
print(urls_list)
p = Pool(processes=process_count)
p.map(get_data, urls_list)
while True:
for data in datas:
get_data()
uj5u.com熱心網友回復:
此腳本使用threading(而不是multiprocessing)打開瀏覽器的多個獨立視窗(實體)。函式中包含的代碼test_instance在每個視窗中同時運行。
import time
from selenium import webdriver
import threading
import json
def test_instance(data):
Options = webdriver.ChromeOptions()
mobile_emulation = {"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/101.0.4951.64 Mobile Safari/535.19"}
Options.add_experimental_option("mobileEmulation", mobile_emulation)
Options.add_argument("--log-level=3")
bot = webdriver.Chrome(options=Options, executable_path="chromedriver.exe")
bot.set_window_size(500, 768)
bot.get("https://www.instagram.com/")
time.sleep(10)
# Login section==========================
print('Logging in...')
bot.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div/div/div/div[3]/button[1]').click()
time.sleep(5)
username_field = bot.find_element_by_xpath('//*[@id="loginForm"]/div[1]/div[3]/div/label/input')
username_field.send_keys(data['username'])
time.sleep(5)
password_field = bot.find_element_by_xpath('//*[@id="loginForm"]/div[1]/div[4]/div/label/input')
password_field.send_keys(data['password'])
time.sleep(5)
bot.find_element_by_xpath('//*[@id="loginForm"]/div[1]/div[6]/button').click()
time.sleep(6)
bot.quit()
f = open('accounts.json',)
data = json.load(f)
f.close()
process_count = 2 # number of tests to run (each test open a separate browser)
thread_list = []
# Start test
for i in range(process_count):
t = threading.Thread(name=f'Test {i}', target=test_instance, args=[data[i]])
t.start()
time.sleep(1)
print(t.name ' started')
thread_list.append(t)
# Wait for all threads to complete
for thread in thread_list:
thread.join()
print('Test completed')
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/479394.html
標籤:Python 硒 多处理 硒铬驱动程序 Instagram
上一篇:RubyonRails,實體變數通過多個請求持續存在
下一篇:Seleniumpython:find_elements_by_tag_name和回圈作業但不是find_element_by_xpath
