Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- import os
- def download_images(username, num_images):
- url = f"https://twitter.com/{username}"
- response = requests.get(url)
- if response.status_code == 200:
- soup = BeautifulSoup(response.content, 'html.parser')
- image_tags = soup.find_all('img')
- image_count = 0
- if not os.path.exists(username):
- os.makedirs(username)
- for img in image_tags:
- image_url = img.get('src')
- if image_url and 'media' in image_url:
- filename = f"{username}_{image_count}.jpg"
- with open(os.path.join(username, filename), 'wb') as f:
- f.write(requests.get(image_url).content)
- image_count += 1
- if image_count >= num_images:
- break
- print(f"Downloaded {image_count} images.")
- else:
- print(f"Failed to retrieve Twitter page for {username}.")
- if __name__ == "__main__":
- username = input("Enter the username of the Twitter account: ")
- num_images = int(input("Enter the number of images to download: "))
- download_images(username, num_images)
Advertisement
Add Comment
Please, Sign In to add comment