7
$\begingroup$

One of my students is doing summer research for a local company to help analyze some of their data. Trouble is, the web interface they provide for downloading the data only allows one to download one customer's info at a time; there are over 3000 customers to analyze.

I'd like to use Mathematica (MMA) to automate this process. Step 1: use MMA to log in to their website. I've looked at many similar posts and still struggle to understand how to use Import or URLFetch or URLExecute to accomplish this.

I can't share the company's URL, though from the page source this seems to be the relevant code:

<div class="container">
<form class="form-signin" role="form" action="index.php?page=login" method="POST">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" name="username" class="form-control" placeholder="Username" required autofocus>
<input type="password" name="password" class="form-control" placeholder="Password" required>
<input type="hidden" name="action" value="login" />
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>

I tried using

URLExecute["https://THEIR_WEB_ADDRESS.com/src/index.php?page=admin", {"username" -> "fooUser", "password" -> "fooPassword"}]

but the response seems to indicate the website doesn't understand the requested page. I also tried variations of "Username" vs "username", etc., to no avail.

Can someone point me in the right direction based on the page source? I know you won't be able to test your own answer as the actual URL is not given.

$\endgroup$
  • $\begingroup$ Based on the form HTML that you posted, the request should be sent to index.php?page=login, not ?page=admin. This will return a cookie, which Mathematica will store away in the variable $Cookies and use for further requests. So when you next run URLRead["https://THEIR_WEB_ADDRESS.com/src/index.php?page=admin"] then that cookie is used to identify you, there's no need to supply username and password to that request. There are many things that can go wrong, but in principle, this is how it should work. $\endgroup$ – C. E. Jun 25 at 20:27
  • $\begingroup$ @C.E. Good catch on the login vs admin part. Still doesn't work; same result. Guess this falls into the "many things that can go wrong" part. Your hands are certainly tied in that I'm giving incomplete info on the url, etc. $\endgroup$ – GregH Jun 25 at 21:25
6
$\begingroup$

This is getting a bit too long for a comment. What you want to do is possible, in principle, but web servers can be picky about how the request should look. What we can do is to try to provide as much information as possible to help Mathematica make an acceptable request. I would start with this:

req = HTTPRequest[
   "https://THEIR_WEB_ADDRESS.com/src/index.php", <|
    Method -> "POST",
    "Query" -> {"page" -> "login"},
    "Body" -> {
      "username" -> "fooUser",
      "password" -> "fooPassword",
      "action" -> "login"
      },
    "ContentType" -> "application/x-www-form-urlencoded"
    |>];

resp = URLRead[req]

The action parameter comes from the HTML for the form.

It would be interesting to know what resp["StatusCode"] returns. If it returns OK, it would also be good to check resp["Cookies"] to see if it returned a cookie. And, of course, you can also check resp["Body"] to see what they sent back.

Please let me know how it goes.

$\endgroup$
  • $\begingroup$ It worked - Thanks! resp["StatusCode"] returns 200 and resp["Body"] returns the HTML of the landing page after Login. Step 2 is to figure out how to follow a link to a Search page, and Step 3 is learn to enter data into the Search field. I may post separately about those if I can't figure it out. $\endgroup$ – GregH Jun 26 at 10:20
  • $\begingroup$ Just an FYI: based on your answer, it seems I am successful in submitting data into the search field and retrieving the results. Many thanks. $\endgroup$ – GregH Jun 26 at 10:38
  • $\begingroup$ @GregH I'm glad it helped. $\endgroup$ – C. E. Jun 26 at 13:21
  • 1
    $\begingroup$ A quick follow-up: was able to algorithmically download over 5,000 records from the website in only a few minutes. Again, many thanks. $\endgroup$ – GregH Jun 30 at 10:19
  • $\begingroup$ Great to hear :) $\endgroup$ – C. E. Jun 30 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.