Python Forum
Downloading images from webpages
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Downloading images from webpages
#1
Hi all,

I have thousands of images to download that I need to automate. I tried a number of solutions I found on the internet, but they all produce an empty file.
Each link looks like this: http://g2w.ubi.com/farcry2/thumb.php?id=...4d995a558a
There is just one small image in the middle but the link is not pointing directly to a file (which I'm guessing the problem is.) In the browser the image can be downloaded as a png file, also, saving the whole page as will also produce that png image. Would anyone have an idea how python could grab and download that image?
Reply
#2
That image is on a non-secure site, so shouldn't be opened.
Reply
#3
The site use JavaScript when click on link to generate source link to image.
Could use Selenium for this,and other way is to look as source code to see what's going on.
Can give a quick demo as this may be not a so easy if new to this.
In source download soup are all id's in a JavaScript array,can use regex to grab all id's.
Then do new call with new url that take id as parameter.
import requests
from bs4 import BeautifulSoup
import re

url = 'http://g2w.ubi.com/farcry2/?page=1'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
# Get all id's for page 1
tumb_id = re.findall(r"id=(.*)';", str(soup), re.MULTILINE)
# First one
params = (('id', tumb_id[0]),)
# Download
response = requests.get('http://g2w.ubi.com/farcry2/thumb.php', params=params)
with open('tumb.png', 'wb') as f:
    f.write(response.content)
Pedroski55 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Downloading a module Xlsxwriter dan789 6 11,285 Jan-26-2019, 02:13 PM
Last Post: dan789
  "if statement" and downloading a dataset Alberto 1 2,502 Jan-25-2018, 01:44 PM
Last Post: ka06059
  Downloading and using pyperclip PMPythonlearner 2 5,058 Dec-31-2017, 04:37 PM
Last Post: PMPythonlearner
  Problem downloading 2.7.8 Mac OSX Benjipincus 2 3,037 Dec-18-2017, 01:33 PM
Last Post: snippsat
  issues downloading xlsxwrite library tenichols 3 9,283 Jun-01-2017, 09:13 PM
Last Post: snippsat
  FTP not downloading files but showing success python_lover 2 4,446 Jan-25-2017, 02:31 PM
Last Post: python_lover

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020