简体   繁体   English

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

[英]Get the name of Instagram profile and the date of post with Python

I'm in the process of learning python3 and I try to solve a simple task.我正在学习python3 ,我尝试解决一个简单的任务。 I want to get the name of account and the date of post from instagram link.我想从 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)

This code works sometimes with links like this https://www.instagram.com/ kingtop But I need it to work also with post of image like this https://www.instagram.com/p/BuxB00KFI-x/此代码有时适用于这样的链接https://www.instagram.com/ kingtop但我需要它也适用于像这样的图像发布https://www.instagram.com/p/BuxB00KFI-x/

That's all what I could make, but this is not working.这就是我所能做的,但这不起作用。 And I can't get the date also.而且我也无法得到日期。 Do you have any ideas?你有什么想法? I appreciate any help.我很感激任何帮助。

I found a way to get the name of account.我找到了一种获取帐户名称的方法。 Now I'm trying to find a way to get an upload date现在我正在尝试找到一种获取上传日期的方法

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')

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

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