简体   繁体   English

下载带有 python 的 Instagram 照片

[英]Download photo of instagram with python

I have a problem that I can't seem to find an answer to.我有一个问题,我似乎无法找到答案。 What I want to achieve: Download the first photo on a persons page.我想要实现的目标:下载人物页面上的第一张照片。

I was going to do it using chromedriver and then get the HTML tag for the scontent-link.我打算使用 chromedriver 来完成,然后获取 scontent-link 的 HTML 标记。 After that I was probably going to write some code to download the photo to a specific folder on my PC using the link.在那之后,我可能会编写一些代码来使用链接将照片下载到我电脑上的特定文件夹中。

The code I wanted to use for reference is:我想用作参考的代码是:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import os

#set up chromedriver
chromedriver = "E:/Instabot/chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
actions = ActionChains(driver)

base_url = "https://www.instagram.com/"
handle="username"
driver.get(base_url+handle)

#go to a picture images[n] is the number of the picture in their feed
images = driver.find_elements_by_class_name("_bz0w")
image_curr = images[1].find_element_by_tag_name("a").get_attribute("href")
driver.get(image_curr)

#Find the HTML class that has the like count
likes = driver.find_elements_by_class_name("Nm9Fw")
Like_list =[]

for l in likes:
   likes = l.find_element_by_css_selector('span').get_attribute("textContent")
   #print(str(likes))
   Like_list.append(likes)
   listToStr = ' '.join([str(elem) for elem in Like_list])
   #print(listToStr)

df = pd.DataFrame({"Likes:": Like_list})
df.to_csv("instagram_likes.txt", index=False)

I used this code to extract the like count from a post.我使用此代码从帖子中提取点赞数。 I am not a skilled or advanced programmer so my code may be messy...我不是一个熟练或高级的程序员,所以我的代码可能很乱......

I hope someone can help me with this problem!我希望有人可以帮助我解决这个问题!

You can use Selenium to get image src but later you need requests or urllib to download it您可以使用 Selenium 来获取图像src但稍后您需要requestsurllib来下载它

import requests

# ... selenium code ... 

img_src = driver.find_element_by_xpath('//div/img').get_attribute("src")
print('img:', img_src)

r = requests.get(img_src)

fp = open('image.jpg', 'wb') # it has to be `bytes` mode
fp.write(r.content) # it has to be `r.content, not `r.text`
fp.close()

EDIT: Full code which I used to test it.编辑:我用来测试它的完整代码。

from selenium import webdriver
import requests

#set up chromedriver
#chromedriver = "E:/Instabot/chromedriver.exe"
#os.environ["webdriver.chrome.driver"] = chromedriver
#driver = webdriver.Chrome(chromedriver)
driver = webdriver.Firefox()

base_url = "https://www.instagram.com/"
handle = "nobody"  # it is real name
driver.get(base_url+handle)

images = driver.find_elements_by_class_name("_bz0w")

# first get all `href` as text
# because after using `driver.get()` it will lost access to objects on page
images_href = []
for img in images:
    href = img.find_element_by_tag_name("a").get_attribute("href")
    images_href.append(href)

# now we can get all images
for number, href in enumerate(images_href):
    driver.get(href)

    img_src = driver.find_element_by_xpath('//div/img').get_attribute("src")
    print('img:', img_src)

    r = requests.get(img_src)
    filename = f'image-{number}.jpg' 
    with open(filename, 'wb') as fp:
        fp.write(r.content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM