See for instance the color picker demo.
In Part 2 we saw how to download an Ajax-powered webpage. However, there was a problem with that approach: sometimes it terminated too quickly, thus it fetched just part of a page. The problem with Ajax is that we cannot tell for sure when a page is completely downloaded.
So, the solution is to integrate some waiting mechanism in the script. That is, we need the following: “open a given page, wait X seconds, then get the HTML source”. Hopefully all Ajax calls will be finished in X seconds. It is you who decides how many seconds to wait. Or, you can analyze the partially downloaded HTML and if something is missing, wait some more.
Let’s see how to fetch the page CP002059.1. If you open it in a browser, you’ll see a status bar at the bottom that indicates the download progress. For me it takes about 20 seconds to fully get this page. By analyzing the content of the page, we can notice that the string “ORIGIN” appears just once, at the end of the page. So we’ll check its presence in a loop and wait until it arrives.
#!/usr/bin/env python from time import sleep from splinter.browser import Browser url = 'http://www.ncbi.nlm.nih.gov/nuccore/CP002059.1' def main(): browser = Browser() browser.visit(url) # variation A: while 'ORIGIN' not in browser.html: sleep(5) # variation B: # sleep(30) # if you think everything arrives in 30 seconds f = open("/tmp/source.html", "w") # save the source in a file print >>f, browser.html f.close() browser.quit() print '__END__' ############################################################################# if __name__ == "__main__": main()
You might be tempted to check the presence of ‘</html>’. However, don’t forget that the browser downloads a plain source first starting with ‘<html><body>…’ until ‘</body></html>’. Then it starts to interpret the source and if it finds some Ajax calls, they will be called, and these calls will expand something in the body of the HTML. So you’ll have ‘</html>’ right at the beginning.
This is not bad but I’m still not fully satisfied. I’d like something like this but without any browser window. If you have a headless solution, let me know. I think it’s possible with PhantomJS and/or Zombie.js but I had no time yet to investigate them.
I hope I can try it one day :)
More info here (in Hungarian).
In the lab we have a photocopier that can scan too. Quite cool, you can precise your email address and it sends you the scanned page in .tif format.
However, pages must be scanned one by one and each of them is sent as a separate .tif file. Each .tif file is around 2.8 MB large with a resolution of 4900 x 7000 pixels. How to resize them and convert them to .jpg files? Gimp is one way but could we solve it in command-line?
Put the .tif files in a folder and create a subfolder called “out”. This way the output won’t be mixed with the input.
for i in *.tif; do echo $i; convert $i -resize 24% out/`basename $i .tif`.jpg; done
Each .tif is made smaller (width around 1200 pixels) and converted to .jpg.
As a final touch, convert the JPGs to a PDF file.
cd out convert *.jpg doc.pdf
Does anyone know how to to resize an image the following way: let width be 1200 pixels and keep the aspect ratio? Above the 24% was the result of a manual computation…
Answer: just use “
convert -resize 1200 in.tif out.jpg“. The output will have width=1200 pixels with the same ratio as the input image. (Thanks Yves for the tip.)
“D3 allows you to bind arbitrary data to a Document Object Model (DOM), and then apply data-driven transformations to the document. As a trivial example, you can use D3 to generate a basic HTML table from an array of numbers. Or, use the same data to create an interactive SVG bar chart with smooth transitions and interaction.” (source)
See also D3 on GitHub.
I haven’t used it yet, this is just a “good-to-know-about-it” note.
See his post here (Sept. 2007) :)
Reactions on reddit here.
Somewhere else I read: ‘ C++ means “increment C by one and use the original value” ‘ :)
Update #2: Check out this post too for an easier solution.
Update #1: This post is deprecated. That damned widget is not shown anymore.
I upgraded to the new look of Gmail but since then I always get a notification in the bottom right corner with “about the new look | send feedback”. Closing it doesn’t help, upon a new log in it’s there again.
Install Adblock Plus and add the following filter:
Does anybody know how to list the current filters in the new look? I can’t find it anywhere.