دا كود تحميل صور من Pinterest :import requests import re import json
def download_pinterest(url): try: # Get the redirect URL from pin.it response = requests.head(url, allow_redirects=True) full_url = response.url
# Get the page content
response = requests.get(full_url)
page_content = response.text
# If no video found, try to find image
image_url = re.search(r'https://i\.pinimg\.com/originals/[^"]+', page_content)
if image_url:
# Download image
img_data = requests.get(image_url.group(0)).content
with open('pinterest_image.jpg', 'wb') as f:
f.write(img_data)
print("Image downloaded successfully")
else:
print("Could not find media URL")
except Exception as e:
print(f"Error: {str(e)}")
url = "https://pin.it/6wnjYwJ94" download_pinterest(url), ودا بيحمل فيديوهات , : import requests import re import json import os
def download_pinterest_video(url): try: response = requests.head(url, allow_redirects=True) full_url = response.url
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(full_url, headers=headers)
page_content = response.text
video_regex = r'"contentUrl":"([^"]+)"'
video_match = re.search(video_regex, page_content)
if video_match:
video_url = video_match.group(1).replace('\\u002F', '/').replace('\\', '')
if video_url:
video_data = requests.get(video_url, headers=headers).content
filename = f'pinterest_video_{os.path.basename(video_url)}'
with open(filename, 'wb') as f:
f.write(video_data)
print(f"Video downloaded successfully as {filename}")
return True
print("No video URL found")
return False
except Exception as e:
print(f"Error: {str(e)}")
return False
url = "https://pin.it/6wnjYwJ94" download_pinterest_video(url)
خليهم كود واحد
I understand you want to combine the two scripts for downloading images and videos from Pinterest into a single code. Here's a unified version that can handle both image and video downloads:
import requests
import re
import os
def download_pinterest_media(url):
try:
# Get the redirect URL from pin.it
response = requests.head(url, allow_redirects=True)
full_url = response.url
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
# Get the page content
response = requests.get(full_url, headers=headers)
page_content = response.text
# Try to find video first
video_regex = r'"contentUrl":"([^"]+)"'
video_match = re.search(video_regex, page_content)
if video_match:
video_url = video_match.group(1).replace('\\u002F', '/').replace('\\', '')
if video_url:
video_data = requests.get(video_url, headers=headers).content
filename = f'pinterest_video_{os.path.basename(video_url)}'
with open(filename, 'wb') as f:
f.write(video_data)
print(f"Video downloaded successfully as {filename}")
return
# If no video found, try to find image
image_url = re.search(r'https://i\.pinimg\.com/originals/[^"]+', page_content)
if image_url:
# Download image
img_data = requests.get(image_url.group(0), headers=headers).content
filename = f'pinterest_image_{os.path.basename(image_url.group(0))}'
with open(filename, 'wb') as f:
f.write(img_data)
print(f"Image downloaded successfully as {filename}")
else:
print("Could not find media URL")
except Exception as e:
print(f"Error: {str(e)}")
# Usage example:
url = "https://pin.it/6wnjYwJ94"
download_pinterest_media(url)
This combined script does the following:
To use this script, you just need to call the download_pinterest_media()
function with a Pinterest URL. It will automatically detect whether it's a video or an image and download it accordingly.
I'll organize the code into a Python package structure suitable for publishing on PyPI. I'll include all necessary files and add usage examples.
This structure includes all the necessary files for a Python package that can be published on PyPI:
src/pinterest_downloader/__init__.py
: Defines the package and its version.src/pinterest_downloader/downloader.py
: Contains the main functionality.examples/download_examples.py
: Provides usage examples.pyproject.toml
: Configuration file for building the package.README.md
: Documentation for the package.LICENSE
: MIT License file.To publish this package on PyPI, you would need to:
pip install build twine
python -m build
python -m twine upload dist/*
Remember to replace placeholder information like your name and email in the pyproject.toml
and LICENSE
files before publishing.