问题
In one of my pages, I ask user for credentials of another website, so that I can log in the website with phantomjs in the background and get some data (because the website still doesn't have API or Oauth).
I can log in most of the time, but sometimes the server got suspicious and the login form contains a captcha field.
I can get the image and return it to the user for him to enter captcha code, but returning from the request, the phantomjs session is ended, and if I start another phantomjs to log in, the captcha is no longer valid.
What is the best approach to handle this? I see some websites do exactly this but don't know how.
BTW, I am using phantomjs in a Django view function.
回答1:
I guess you need a phantomjs instance that you run asynchronously from django on the first request. The phantomjs instance continues running.
You can use the webserver module of phantomjs to communicate with the target phantomjs instance from python after it is started.
Inside the first request you stall and wait until the phantomjs is on the intended page with the captcha. If there is a captcha then return it to django and finish the first request.
In the django server you would map currently active users to phantomjs instance through the port. On the next request with the solved captcha you also exit the phantom instance, but only if it was successful.
Sadly, I don't know enough about django request management to you show some code.
来源:https://stackoverflow.com/questions/25218984/how-to-pass-login-form-with-captcha-field-using-phantomjs