Hey everyone! I’m new to coding and could use some help. I’m trying to figure out how to upload several photos to Telegraph using Python. Is there a way to do this?
I saw there’s a create_page method, but I’m not sure how to use it for multiple images. Can I just send the photo links to the content parameter? Or is there a better way?
Here’s a simple example of what I’m trying to do:
from telegraph import Telegraph
telegraph = Telegraph()
photo_urls = [
'https://example.com/photo1.jpg',
'https://example.com/photo2.jpg',
'https://example.com/photo3.jpg'
]
# How do I upload these photos?
# page = telegraph.create_page(title='My Photos', content=???
print('Photos uploaded successfully!')
Any tips or advice would be super helpful. Thanks in advance!
I’ve worked with Telegraph API before, and there’s a neat trick you can use to simplify the process. Instead of creating the content list manually, you can leverage Telegraph’s upload_file method. Here’s how:
This approach downloads each image and uploads it directly to Telegraph’s servers, ensuring compatibility and avoiding potential issues with external image hosts. It’s a bit more robust and gives you more control over the process.
I’ve actually done something similar recently for a project. Here’s what worked for me:
You’re on the right track with the create_page method, but you need to structure the content properly. Telegraph expects the content as a list of node elements. For images, you can use the ‘img’ tag with the ‘src’ attribute.