I've written some code using Python 3 to scrape movie names, links to the movie posters, and finally save the pictures on the local drive after downloading them from a web page.
I have used two functions to accomplish the whole task. I've tried my best to make the process clean. It is working great now. Any suggestion as to the betterment of this script will be very helpful for me. Thanks in advance. Here is the working code:
import requests from lxml import html import os url = "https://www.yify-torrent.org/search/1080p/" def ImageScraper(link): response = requests.session().get(link).text tree = html.fromstring(response) for title in tree.xpath('//div[@class="mv"]'): movie_title = title.findtext('.//h3/a') image_url = title.xpath('.//img/@src') image_url = "https:" + image_url image_name = image_url.split('/')[-1] SavingImages(movie_title, image_name, image_url) def SavingImages(movie_name, item_name, item_link): response = requests.session().get(item_link, stream = True) if response.status_code == 200: os.chdir(r"C:\Users\ar\Desktop\mth") with open(item_name, 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) print(movie_name, item_link) ImageScraper(url)