4
$\begingroup$

Today, I read a good web page about love which owns many beautiful pictures. It is fussy for me to download every picture hand-by-hand, so I'd like to use Mathematica to do this case automatically.

I searched the documentation, then I found Import is a relate function.

Trial:

Import["http://mp.weixin.qq.com/s?__biz=MzA5NDY3OTAyNA==&mid=205311623&idx=1&sn=7b565a6ed5789732f698d5a6b4c5c652&scene=2&from=timeline&isappinstalled=0#rd", "Images"]

enter image description here

Obviously, this trial was failed.

Question

Is it possible to download pictures from web page automatically? Any suggestions or hints?

$\endgroup$
  • 2
    $\begingroup$ Many (but by no means all) modern web sites rely heavily on mechanisms such as Javascript. HTML parser that Import uses to find images on a web page doesn't understand these mechanisms, and can find only those images directly linked from the HTML document. In this case, they are the pen and progress indicator, rest of images are delivered through other means. I don't believe there is a built-in way to emulate a whole browser in built-in functionality of current Mathematica. $\endgroup$ – kirma Mar 8 '15 at 6:28
  • 1
    $\begingroup$ kylen314.com/archives/1647 This article maybe helpful $\endgroup$ – wuyingddg Mar 8 '15 at 6:39
  • 2
    $\begingroup$ @Pickett This is of course true, but unless we want to be overly abstract, it has to be recognized that collecting data - for instance all images an user might consider to be present on a web page while she reads it - needs to emulate a browser (which is computably feasible for Mma), but might even require emulating user interaction, such as scrolling the page. Solution by wuyingddg is valid in this specific situation, but by no means universal solution to a general, unfortunate problem that arises in the modern Web. $\endgroup$ – kirma Mar 8 '15 at 8:45
  • 1
    $\begingroup$ @kirma For most websites the case is that the logic Javascript carries out is quite simple and what you really want to get your hands on is the input data (e.g. image URLs.) This is (for reusability and ease-of-use reasons) stored in the HTML itself, and usually in well known places such as data attributes. With jSoupLink we can extract the input data with the same syntax that you would use in Javascript, so it's not harder or easier in Mathematica than in Javascript. $\endgroup$ – C. E. Mar 8 '15 at 9:21
  • 1
    $\begingroup$ @kirma There are some websites that are very complicated with a lot of non-trivial Javascript, but I classify those as web applications (think Google Docs). In those cases it can be hard to locate what you're looking for. But the vast majority of websites are not like that. $\endgroup$ – C. E. Mar 8 '15 at 9:23
9
$\begingroup$

Based on this article: kylen314.com/archives/1647

st = Import[
   "http://mp.weixin.qq.com/s?__biz=MzA5NDY3OTAyNA==&mid=205311623&\
idx=1&sn=7b565a6ed5789732f698d5a6b4c5c652&scene=2&from=timeline&\
isappinstalled=0#rd", "XMLObject"];
PicAddress = 
  Cases[st, 
   XMLElement["img", {"data-src" -> src_, ___}, {}] :> src, {0, 
    Infinity}];
Import /@ PicAddress

enter image description here

$\endgroup$

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.