-5
\$\begingroup\$

Recreational languages win at code golf too much. This challenge is simple, when using higher level functions.

The task is as follows:

Given any valid url, which is guaranteed to result in a valid HTML document (for example https://en.wikipedia.org/wiki/Ruby_on_Rails, which has 9 images), count the number of images displayed from the resultant webpage.

Only images visible in the HTML code are counted, marked by the HTML <img> tag.

Websites that can continually load images (Google Images) are not valid tests.

\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – Mego Apr 12 '18 at 12:47
  • 2
    \$\begingroup\$ This is code-golf, right? The shortest code wins, right? Then why does a 61 byte answer beat an 10 byte answer? If my answer is invalid in some way, just comment on my post. \$\endgroup\$ – NoOneIsHere Apr 13 '18 at 22:55
2
\$\begingroup\$

JavaScript, 61 bytes

As per this consensus, the code needs to run under the same domain as the page being requested, to avoid CORS issues. Returns a Promise conatining the count.

u=>fetch(u).then(r=>r.text()).then(t=>t.split`<img`.length-1)

Thanks to tsh for pointing out some code I forgot to update before posting, saving 3 bytes.


Try it

(f=
u=>fetch(`https://crossorigin.me/`+u).then(r=>r.text()).then(t=>t.split`<img`.length-1)
)(i.value=`https://codegolf.stackexchange.com/questions/161669/`).then(x=>o.innerText=x);b.addEventListener(`click`,_=>f(i.value).then(x=>o.innerText=x),0)
<input id=i><button id=b>Count</button><pre id=o></pre>

\$\endgroup\$
  • \$\begingroup\$ Why not .match? or .split"<img"? \$\endgroup\$ – tsh Apr 10 '18 at 11:05
  • \$\begingroup\$ .match works out longer in order to handle the case of their being no images. You're right on the second count, though; forgot to change it before posting. Thanks. \$\endgroup\$ – Shaggy Apr 10 '18 at 11:07
  • \$\begingroup\$ *there! Dunno how I did that! \$\endgroup\$ – Shaggy Apr 10 '18 at 22:12
2
\$\begingroup\$

CJam, 10 bytes

lg"<img"e=

Does not work online.

Description:

l          # read line
 g         # open url and get html
  "<img"   # this string
        e= # count arg2 in arg1
\$\endgroup\$
  • 1
    \$\begingroup\$ @downvoters what did I do wrong? \$\endgroup\$ – NoOneIsHere Apr 10 '18 at 4:13
  • 1
    \$\begingroup\$ No idea. / Anyone who hates golfing languages is likely to know none of them. (otherwise they would know how hard it is to golf in those languages) Bash is not even a golfing language... \$\endgroup\$ – user202729 Apr 10 '18 at 4:53
  • 1
    \$\begingroup\$ Can you add a TIO and/or an explanation? I don't know CJam and can't figure out how this is reading in the HTML. \$\endgroup\$ – Shaggy Apr 10 '18 at 9:39
2
\$\begingroup\$

Python 3 + Selenium + Firefox, 124 bytes

from selenium.webdriver import*
d=Firefox()
d.get(input())
c=len(d.find_elements_by_xpath('//img'))
d.quit()
print(c)

Notice that https://en.wikipedia.org/wiki/Ruby_on_Rails report 8 instead of 9. This is due to one of the <img> tag is contained by <noscript>, which is regarded as comment by a browser who enabled script.

Changing webdriver.Firefox to webdriver.Ie may save 5 bytes... But I just dislike IE.

How to run:

  • First install Python 3, and Firefox
  • install Selenium by pip install selenium
  • Download gecko driver and put it to your path
  • Run this script
  • A Firefox will be started, ignore it and input the url to stdin
  • The result will be shown on stdout
\$\endgroup\$
  • \$\begingroup\$ A manual count confirms that there are 9 img tags on that page. \$\endgroup\$ – Shaggy Apr 10 '18 at 9:38
  • \$\begingroup\$ @Shaggy One of them is in <noscript> tag which will not be rendered if browser support javascript; 3 of them are invisible to user. So there are 5 image displayed. \$\endgroup\$ – tsh Apr 10 '18 at 9:55
  • \$\begingroup\$ "Only images visible in the HTML code are counted, marked by the HTML <img> tag." \$\endgroup\$ – Shaggy Apr 10 '18 at 9:56
  • \$\begingroup\$ Yes, and also "count the number of images displayed from the resultant webpage". I read this as "only displayed <img> tags are count. \$\endgroup\$ – tsh Apr 10 '18 at 9:59
  • \$\begingroup\$ You're right, there's a contradiction there. I suggest asking for clarification. \$\endgroup\$ – Shaggy Apr 10 '18 at 10:01
1
\$\begingroup\$

Python 2 + BeautifulSoup, 103 bytes

import bs4 as B
import urllib as U
print len(B.BeautifulSoup(U.urlopen(input()).read()).findAll('img'))

Can't do a TIO as BeautifulSoup is not available there. This reports 9 for https://en.wikipedia.org/wiki/Ruby_on_Rails.

Note: A warning is displayed before the number as a parser is not specified and lxmlis taken as the default. Adding the lxml parser explicitly suppresses the warning but costs another 7 bytes.

import bs4 as B
import urllib as U
print len(B.BeautifulSoup(U.urlopen(input()).read(),'lxml').findAll('img'))
\$\endgroup\$
-1
\$\begingroup\$

Bash, grep, and wc, 45 28 bytes

curl $1|grep -o "<img"|wc -l

-17 bytes thanks to tsh

Explanation:

curl $1                      # download html from first parametre
       |grep -o "<img"       # find all <img strings
                      |wc -l # count lines
\$\endgroup\$
  • 3
    \$\begingroup\$ What if there are multiple <img> tags on a line? \$\endgroup\$ – nimi Apr 9 '18 at 23:06
  • \$\begingroup\$ @downvoters what did I do wrong? \$\endgroup\$ – NoOneIsHere Apr 10 '18 at 4:13
  • \$\begingroup\$ grep -o '<img' should works if you just want <img substring. \$\endgroup\$ – tsh Apr 10 '18 at 7:00
  • \$\begingroup\$ @nimi - if that's the case then the person who made the web site should be a code golfer and not a web developer :-) \$\endgroup\$ – ElPedro Apr 10 '18 at 20:25
  • 1
    \$\begingroup\$ @ElPedro No need of code golfer, just a minify tool should be enough. \$\endgroup\$ – tsh Apr 11 '18 at 4:51

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