Frage

The issue:

I have written a ton of code (to automate some pretty laborious tasks online), and have used the mechanize library for Python to handle network requests. It is working very well, except now I have encountered a page which I need javascript functionality... mechanize does not handle javascript.

Proposed Solution:

I am using PyQt to write the GUI for this app, and it comes packaged with QtWebKit, which DOES handle javascript. I want to use QtWebKit to evaluate the javascript on the page that I am stuck on, and the easiest way of doing this would be to transfer my web session from mechanize over to QtWebKit.

I DO NOT want to use PhantomJS, Selenium, or QtWebKit for the entirety of my web requests; I 100% want to keep mechanize for this purpose. I'm wondering how I might be able to transfer my logged in session from mechanize to QtWebKit.

Would this work?

  • Transfer all cookies from mechanize to QtWebView
  • Transfer the values of all state variables (like _VIEWSTATE, etc.) from mechanize to QWebView (the page is an ASP.net page...)
  • Change the User-Agent header of QWebView to be identical to mechanize...

I don't really see how I could make the two "browsers" appear more identical to the server... would this work? Thanks!

War es hilfreich?

Lösung

Since nobody answered, I will post my work-around.

Basically, wanted to "transfer" my session from Mechanize (the python module) to the QtWebKits QWebView (PyQt4 module) because the vast majority of my project was automated headless, but I had encountered a road block where I had no choice but to have the user manually enter data into a possible resulting page (as the form was different each time depending on circumstances).

Instead of transferring sessions, I met this requirement by utilizing QWebViews javascript functionality. My method went like this:

  1. Load page in Mechanize, and save the downloaded HTML to a local temporary file.
  2. Load this local file in QWebView.
  3. The user can now enter required data into the local copy of this page.
  4. Locate the form fields on this page, and pull the data the user entered using javascript. You can do this by getting the main frame object for the page (QWebView->Page()->MainFrame()), and then evaluating javascript code to accomplish the above task (use evaluateJavaScript()).
  5. Take the data you have extracted from the form fields, and use it to submit the form with the connection you still have open with mechanize.

That's it! A bit of a work-around, but it works none-the-less :\

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top