使用Python获取Instagram个人资料的名称和发布日期

问题描述 投票:0回答:1

我正在学习python3,我尝试解决一个简单的任务。我想从instagram链接获取帐户名称和发布日期。

import requests
from bs4 import BeautifulSoup

html = requests.get('https://www.instagram.com/p/BuPSnoTlvTR')
soup = BeautifulSoup(html.text, 'lxml')
item = soup.select_one("meta[property='og:description']")
name = item.find_previous_sibling().get("content").split("•")[0]
print(name)

这段代码有时会像这样的https://www.instagram.com/kingtop这样的链接工作但是我需要它也可以像这样的https://www.instagram.com/p/BuxB00KFI-x/这样的图像一起工作

这就是我所能做的,但这不起作用。我也无法得到约会。你有什么想法?我感谢任何帮助。

python-3.x beautifulsoup instagram
1个回答
1
投票

我找到了获取帐户名称的方法。现在我正试图找到一种获取上传日期的方法

import requests
from bs4 import BeautifulSoup
import urllib.request
import urllib.error
import time
from multiprocessing import Pool
from requests.exceptions import HTTPError

start = time.time()

file = open('users.txt', 'r', encoding="ISO-8859-1")
urls = file.readlines()
for url in urls:
url = url.strip ('\n')
try:
    req = requests.get(url)
    req.raise_for_status()
except HTTPError as http_err:
    output = open('output2.txt', 'a')
    output.write(f'не найдена\n')  
except Exception as err:
    output = open('output2.txt', 'a')
    output.write(f'не найдены\n')  
else:
    output = open('output2.txt', 'a')
    soup = BeautifulSoup(req.text, "lxml")
    the_url = soup.select("[rel='canonical']")[0]['href']
    the_url2=the_url.replace('https://www.instagram.com/','')
    head, sep, tail = the_url2.partition('/')
    output.write (head+'\n')
© www.soinside.com 2019 - 2024. All rights reserved.