【问题标题】:Python Get the URL of all posts of an Instagram userPython 获取 Instagram 用户所有帖子的 URL
【发布时间】:2020-09-18 10:58:23
【问题描述】:

我编写了一小段代码来从 Instagram 下载图片。代码如下:

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as req
import urllib.request as reqq
from selenium import webdriver
import os


browser = webdriver.Chrome("D:\\Python_Files\\Programs\\chromedriver.exe")

url = "https://www.instagram.com/p/CFRY7X2AnOx/"

browser.get(url)
image_url = browser.find_element_by_class_name('KL4Bh').find_element_by_tag_name('img').get_attribute('src')

reqq.urlretrieve(image_url,"D:\\instaimg.jpg")

这很好用。但这只能下载一个图像。有什么方法可以获取用户所有帖子的网址,以便下载用户发布的所有图片?

【问题讨论】:

  • 您使用的链接指向此人的单个帖子。先去个人资料然后获取所有帖子的图片不是更好吗?
  • 我该怎么做?
  • 你问的问题太宽泛了。您想要每个帖子中的所有图像还是只想要第一张图像?他们发布的视频呢?
  • 我实际上已经修改了我的代码,以便可以下载图像和视频。现在这不是问题。我只想获取用户发布的所有帖子的链接。
  • 我想我明白了。所有帖子的 URL 都在 "v1Nh3 kIKUG _bz0w" 类的 div 标签下,对吗?

标签: python web beautifulsoup instagram screen-scraping


【解决方案1】:

这应该可行:

browser.get("https://instagram.com/"+username)

# Click on the first post
browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div[3]/article/div/div/div[1]/div[1]/a").click()

# Wait a second or two for the post to load using either the `time` module or whatever way you want

posts = []
while True:
    try:
        posts.append(browser.current_url)
        arrow = browser.find_element_by_class_name("coreSpriteRightPaginationArrow")
        arrow.click()
    except:
        # Arrow was not found, so you must be on the last post
        break

print(posts)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多