필터 지우기
필터 지우기

How to know whether it is an image or a website with a given url?

조회 수: 3 (최근 30일)
Salad Box
Salad Box 2017년 11월 21일
댓글: Guillaume 2017년 11월 22일
Hi
I have a few urls, each of which begin with 'http://' and end with '.jpg'.
Some lead to images and others lead to websites (e.g. the above url).
Is there a way to know whether each url leads to an image or a website? If it leads to an image, save it; if it leads to a website, do nothing.
Can anyone help?

채택된 답변

Guillaume
Guillaume 2017년 11월 22일
편집: Guillaume 2017년 11월 22일
The way matlab (or any other web client) determine the type content of a uri is by looking at the Content-Type property returned in the header of the get (or post) request that matlab (or any other web client) send when you request that page. All of this is hidden from you by webread and websave.
Since R2016b, matlab gives you functions to get down to the nitty gritty of the http protocol. The following should work:
queryurl = 'http://anguerde.com/pics/main/2/218886-active.jpg'; %replace as required
uri = matlab.net.URI(queryuri);
request = matlab.net.http.RequestMessage;
response = request.send(uri);
contenttype = response.Header.getFields('Content-Type');
If the uri points to an image, then Content-Type should contain 'image/something', so you could do
isimage = contains(contenttype.Value, 'image/')
Note that if you need to pass query parameters, or log into the web page, or some other things, there's a lot more work that needs to be performed before the above would work.

추가 답변 (2개)

Rik
Rik 2017년 11월 21일
Would websave give an error if you try to save a page as a single file (didn't test)? In that case you could just use a try-catch-block. If not, you can just save it, and do some operations that will only work on images (like imread), wrapping that in a try-catch-block.
  댓글 수: 1
Salad Box
Salad Box 2017년 11월 22일
Unfortunately websave didn't give an error. As all my urls look like the example shown above, and I named all the filename something like 'apple01.jpg', or 'apple02.jpg', therefore, in case the url is an image, websave will save it as a '.jpg' file; in case the url is a website, websave will save it as a '.html' file, so I will get the file name look like 'apple01.jpg.html'. Websave saves anything on the web, if its an image, it saves the image, if its a website, websave saves the whole website. Maybe that's why its called websave not imagesave. So no it didn't give any errors, instead it just save the web.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2017년 11월 22일
Try imread(). If it throws an error, it's not an image.
filename = 'http://anguerde.com/pics/main/2/218886-active.jpg';
try
rgbImage = imread(filename)
imshow(rgbImage); % Or whatever you plan on doing with the image
catch ME
fprintf('Sorry, but %s is a website, not an image.\n', filename);
end
If you don't want to view or save the image (or try to), then Guillaume's way is more direct, and maybe faster.
  댓글 수: 1
Guillaume
Guillaume 2017년 11월 22일
You could also simply check the extension returned by websave. In both case, the whole page is downloaded and saved to disk ( imread calls websave anyway) so yes this is going to be slower than my method. What imread (or checking the extension returned by websave) got going for it is that it's a lot more error proof than my method.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Web Services from MATLAB Using HTTP에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by