Nirvana
asked on
Download images from web
Download pictures from web
I have to go to a website "https://www.kontorsgiganten.se/" in the search menu give a number for example "843046" and download image to specific folder. How can I automate this.
The numbers are in a excel sheet and i have about 500 everyday that i have to download.
Could you please help me
I have tried this "https://stackoverflow.com/questions/17436646/excel-2013-vba-error" but not working.
I have to go to a website "https://www.kontorsgiganten.se/" in the search menu give a number for example "843046" and download image to specific folder. How can I automate this.
The numbers are in a excel sheet and i have about 500 everyday that i have to download.
Could you please help me
I have tried this "https://stackoverflow.com/questions/17436646/excel-2013-vba-error" but not working.
Option Explicit
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByRef pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserve As Long, _
ByRef lpfnCB As LongPtr) _
As LongPtr
Dim Ret As LongPtr
'~~> This is where the images will be saved. Change as applicable
Const FolderName As String = "D:\Temp\"
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim strPath As String
'~~> Name of the sheet which has the list
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow '<~~ 2 because row 1 has headers
strPath = FolderName & ws.Range("A" & i).Value & ".jpg"
Ret = URLDownloadToFile(0, ws.Range("B" & i).Value, strPath, 0, 0)
If Ret = 0 Then
ws.Range("C" & i).Value = "File successfully downloaded"
Else
ws.Range("C" & i).Value = "Unable to download the file"
End If
Next i
End Sub
ASKER
Sorry Fabrice, I took that site(any random site would do) as an example. all I need is to search for particular id and download image. it can be Amazon too
1) As Fabrice said, first ensure you have the legal right to download the photos.
2) To actually download the images, you can't use your code. This site uses Javascript to build pages, so you'll either have to...
a) Build in Javascript into your code execution for this site, which will be complex + only work in VBA.
b) Use PhantomJS for site interaction, which will be simple + work in any language.
The primary factor here is it looks like you'll have to arrange to execute Javascript on every page, to ensure correct content is coming back.
Here's how to determine this...
For your search, the https://images.tctc.se/9nk 95zrrqm6yu es3ds5txll 4hc93evwf_ 143_176.jp g is returned in a browser (Javascript executed).
Using curl (no Javascript execution), some image related data is returned + looks like Javascript must run to return the image file...
Start with PhantomJS, as your code will be running in a few seconds, to see if this solves the problem.
2) To actually download the images, you can't use your code. This site uses Javascript to build pages, so you'll either have to...
a) Build in Javascript into your code execution for this site, which will be complex + only work in VBA.
b) Use PhantomJS for site interaction, which will be simple + work in any language.
The primary factor here is it looks like you'll have to arrange to execute Javascript on every page, to ensure correct content is coming back.
Here's how to determine this...
For your search, the https://images.tctc.se/9nk
Using curl (no Javascript execution), some image related data is returned + looks like Javascript must run to return the image file...
imac> curl -s 'https://www.kontorsgiganten.se/sok?q=843046' | grep jpg
<img ng-if="item.object.image" ng-src="{{:: item.object.image|imageUrl:'img_'+item.object.id+'.jpg' }}" id="productImage{{:: item.object.id }}" class="productImage"/>
<img ng-if="!item.object.image" src="/img/xno_image.jpg.pagespeed.ic.kZRCEsVd9r.jpg" id="productImage{{ item.object.id }}" class="productImage"/>
Start with PhantomJS, as your code will be running in a few seconds, to see if this solves the problem.
Ugh... More complex than at first glance...
The above suggests that other considerations must occur, like interacting with the entire site to...
1) Start a session.
2) Ensure the cart is somehow instantiated.
3) Traverse the site, till search is returned.
4) Then pull the image, which might require more Javascript.
Likely take a good bit of time to accomplish what you're after.
imac> phantomjs ~/bin/scrape.js 'https://www.kontorsgiganten.se/sok?q=843046'
[object Object]
resultCount: 0
false
cart loaded
initialized
cart was changed!
properties was changed!
[object Object]
TypeError: undefined is not an object (evaluating 'document.getElementsByClassName('target-class')[0].innerHTML')
phantomjs://webpage.evaluate():2
phantomjs://webpage.evaluate():3
The above suggests that other considerations must occur, like interacting with the entire site to...
1) Start a session.
2) Ensure the cart is somehow instantiated.
3) Traverse the site, till search is returned.
4) Then pull the image, which might require more Javascript.
Likely take a good bit of time to accomplish what you're after.
ASKER
is there a other solution than PhantomJS, as I am do not have rights to install
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
The fact that it is on a web site doesn't mean it is rights free.