【问题标题】:How to make this program use instagram pic urls and download? [duplicate]如何让这个程序使用 instagram pic url 并下载? [复制]
【发布时间】:2021-03-14 10:19:31
【问题描述】:

程序的目标是获取用户给定的 instagram url 并允许下载和保存图片。 我已经完成了主要部分,但无法理解如何进一步使用过滤后的正确 url 下载图片并将其保存在我的计算机上。

到目前为止我的代码: 编辑:我添加了一个下载行,但似乎无法获得正确的文件类型?我的意思是它可以保存为我想要的任何内容,但我无法打开它:

import requests
import re
import shutil

def get_response(url):
    r = requests.get(url)
    while r.status_code != 200:
        r.raw.decode_content = True
        r = requests.get(url, stream = True)
    return r.text

def prepare_urls(matches):
    return list({match.replace("\\u0026", "&") for match in matches})

url = input('Enter Instagram URL: ')
response = get_response(url)

vid_matches = re.findall('"video_url":"([^"]+)"', response)
pic_matches = re.findall('"display_url":"([^"]+)"', response)

vid_urls = prepare_urls(vid_matches)
pic_urls = prepare_urls(pic_matches)

if vid_urls:
    print('Detected Videos:\n{0}'.format('\n'.join(vid_urls)))
    print("Can't download video, the provided URL must be of a picture.")
    
if pic_urls:
    print('Detected Pictures:\n{0}'.format('\n'.join(pic_urls)))
        from urllib.request import urlretrieve
        dst = 'Instagram picture.jpg'
        urlretrieve(url, dst)
#EDIT ^

if not (vid_urls or pic_urls):
    print('Could not recognize the media in the provided URL.')
    



【问题讨论】:

    标签: python python-3.x download


    【解决方案1】:

    我认为这可能会有所帮助...

    import requests
    from bs4 import BeautifulSoup as bs
    import json
    import os.path
    
    insta_url = 'https://www.instagram.com'
    inta_username = input('enter username of instagram : ')
    
    response = requests.get(f"{insta_url}/{inta_username}/")
    
    if response.ok:
        html = response.text
        bs_html = bs(html, features="lxml")
        bs_html = bs_html.text
        index = bs_html.find('profile_pic_url_hd')+21
        remaining_text = bs_html[index:]
        remaining_text_index = remaining_text.find('requested_by_viewer')-3
        string_url = remaining_text[:remaining_text_index].replace("\\u0026", "&")
    
        print(string_url, "\ndownloading...")
    
    while True:
        filename = 'pic_ins.jpg'
        file_exists = os.path.isfile(filename)
    
        if not file_exists:
            with open(filename, 'wb+') as handle:
                response = requests.get(string_url, stream=True)
                if not response.ok:
                    print(response)
                for block in response.iter_content(1024):
                    if not block:
                        break
                    handle.write(block)
        else:
            continue
        break
    print("completed")
    

    您可以通过更改文件名变量来更改下载的图像的名称

    【讨论】:

    • 这似乎对我不起作用,我得到了各种各样的错误,据我所知,它下载的个人资料图片不是实际的帖子
    • 相同的代码在我的系统上工作,我使用的是 python 3.8。你用的是哪个版本的python?
    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 2017-04-29
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多