【问题标题】:Get the name of Instagram profile and the date of post with Python使用 Python 获取 Instagram 个人资料的名称和发布日期
【发布时间】:2019-03-10 17:25:06
【问题描述】:

我正在学习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】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多