0

Python noobie.

I'm trying to make Python select a portion of my screen. In this case, it is a small window within a Firefox window -- it's Firebug source code. And then, once it has selected the right area, control-A to select all and then control-C to copy. If I could figure this out then I would just do the same thing and paste all of the copies into a .txt file.

I don't really know where to begin -- are there libraries for this kind of thing? Is it even possible?

user1472747
  • 529
  • 3
  • 10
  • 25
  • mechanize python should do what you want ... – Joran Beasley Mar 25 '13 at 23:35
  • @JoranBeasley doesn't mechanize just fake a browser? I don't think it would allow for interaction with a firebug (firefox addon) window. – ernie Mar 25 '13 at 23:40
  • 1
    What are you really trying to accomplish? There may be a way to get the data you're looking for, without needing to screen-scrape from Firebug . . . – ernie Mar 25 '13 at 23:41
  • Well, my other question is here: http://stackoverflow.com/questions/15623388/using-selenium-with-python-to-extract-javascript-generated-html-firebug/15624933#comment22168280_15624933 but I'm not getting much response. This question is just kind of a backup solution I guess. I could always just use a macro that records my own mouse/keyboard movements, but that would be really, really slow for the data mining I'm trying to do. – user1472747 Mar 25 '13 at 23:46
  • You're definitely going to have to use Selenium or similar for this if that image map is getting loaded via AJAX. I'm assuming you have to click on something, and then that image map is displayed, correct? Your Se script would need to mimic that click, wait for the map to load, then get that new element. Alternatively, that could be greyed out due to the element being hidden, so you might just be better off getting the HTML and then parsing that. – ernie Mar 25 '13 at 23:53
  • Yeah, that's all I want. I just want the HTML (all of it), and then I can parse it later. And no, all of that stuff loads up when I load the webpage. I don't need to click anything. So that should make it easier. – user1472747 Mar 26 '13 at 00:33
  • Does this really need to be all in Python? Can you write a Greasemonkey script (in JavaScript) that drives Firefox from inside and saves files for Python to deal with later, instead of trying to drive Firefox from outside? Because that's almost always easier. – abarnert Mar 26 '13 at 01:14
  • Alternatively, if you're just aiming to click a particular mouse location on the screen (absolute, or relative to a window you can find by name) and send some keystrokes, and you only need Windows (you didn't specify your platform, but you sound like a Windows user…), it may be only a few lines of [`pywinauto`](http://code.google.com/p/pywinauto/) code. – abarnert Mar 26 '13 at 01:16
  • 1
    Finally, why is this tagged asp.net? (Unless you're writing the server code as well as the client code… in which case just expose whatever you're trying to transfer through a simpler REST or RPC interface so you can get it trivially from Python.) – abarnert Mar 26 '13 at 01:18

2 Answers2

0

I would look into PyQt or PySide which are Python wrapper on TOp of Qt. Qt is a big monster but it's very well documented and i'm sure it will help you further in your project once you grabbed your screen section.

  • Qt doesn't have an API to interact with Firefox, or to send low-level mouse/keyboard events to other apps, so… what exactly what you use it for? – abarnert Mar 26 '13 at 01:13
0

As you've mentioned in the comments, the data is all in the HTML to start (I'm guessing it's greyed out in your Firebug screenshot since it's a hidden element). This approach avoids the complexity of trying to automate a browser. Here's a rough outline of how I would get the data:

  • Download the HTML for the whole page - I'd do this manually at first (i.e. File > Save from a browser), and if there are a bunch of pages you want to process, figure out how to download all the pages you want later. If you want to use python for this part, I'd recommend urllib2. The URLs for each page are probably pretty structured, so you could easily store them in a list, and download each one and save it locally. .

  • Write a script to parse the HTML - don't use regex. Since you're using Python, use something like Beautiful Soup, which will create a nice object representation of the page, and then you can get the elements you want.

You mention you're new to python, so there's definitely going to be a learning curve around this, but this actually sounds like a pretty doable project to use to learn some more python.

If you run into specific obstacles with each step, start a new question with a bit of sample code, showing what you're trying to accomplish, and people will be more than willing to help out.

Community
  • 1
  • 1
ernie
  • 6,356
  • 23
  • 28
  • Sorry, I must not have said that right. The data doesn't appear to be in the HTML. If I look at the source, it's not in there. If I grab the page's HTML, the data isn't in there either. – user1472747 Mar 29 '13 at 18:06