一次使用多个帐户和selenium + requests实例

时间:2018-10-06 22:06:36

标签: python python-3.x selenium python-requests subprocess

from selenium import webdriver
import requests


driver = webdriver.Chrome()

#login to website using selenium and get cookies

cookievar = driver.get_cookies()


#send requests using cookies scraped from selenium webdriver

r = post(url, formData, headers=headers0, proxies=proxies, verify=False)

我的代码可以很好地使用一个帐户,但是现在我希望它能够使用多个帐户,此脚本一次只能运行1个帐户,而我想同时运行3-4个帐户使用网络驱动程序和请求

有人可以帮忙吗?欢迎提出建议,谢谢

1 个答案:

答案 0 :(得分:1)

这就是我在评论时的想法,但这只是一个演示。除了multiprocessingthreading之外,您还可以尝试concurrent.futures

from selenium import webdriver
import requests
from multiprocessing import Pool

driver = webdriver.Chrome()
clist = {}
#login to website using selenium and get cookies

def relogin(user,pwd):
    ...
    cookievar = driver.get_cookies()
    clist[user] = cookievar
    ...
    driver.delete_all_cookies()

userlist = [("userA","pwdA"),("userB","pwdB")]

for user,pwd in userlist:
    relogin(user,pwd)

driver.close()

#send requests using cookies scraped from selenium webdriver
def post_data(url,formData,proxies,headers = headers0):
    r = post(url, formData, headers=headers, proxies=proxies, verify=False)
    ...

def start(number=len(userlist)):
    p = Pool(number)
    result = p.map_async(post_data , [(url,formData,proxies),
                            (url2,formData2,proxies2,headers2)])
    print(result.get())

if __name__ == '__main__':
    start()