【问题标题】:Connect to instagram with request通过请求连接到 Instagram
【发布时间】:2021-05-25 19:41:18
【问题描述】:

我正在编写一个新项目来提高我的 Python 技能。它获取 instagram 个人资料的数据并对其进行处理。直到现在它起作用了,我可以访问不同的页面。但我认为通过执行太多请求,服务器已将我注销。我不知道如何连接 python 请求,所以我被阻止了。 任何想法 ? 谢谢

这是我的代码:

from bs4 import BeautifulSoup
import requests
import json
import os

def get_info_insta(username):

    url = f'https://www.instagram.com/{username}/'

    headers = {
        'User-Agent': 'Mozilla/5.0',
        'email': 'email',
        'pass' : 'password'
    }
    response = requests.get(url, headers=headers)
    session = requests.Session()
    session.post(url, data=headers)


    soup = BeautifulSoup(response.text, features="html.parser")

    title = str(soup.find("meta",  property="og:description"))
    print(soup)
    title = title.split("-")[0]
    #title = title.split("=")[1]
    #title = title.split('"')[1]
    return title


title = get_info_insta('javascripthub')  #ewa.english   javascripthub



def treat_data(): 

    title = get_info_insta('ewa.english')  #ewa.english   javascripthub


    #on coupe pour avoir les différentes variables
    title_set = title.split(",")

    title_number = len(title.split(",")) #on compte en combien de fois coupé pour savoir si il y a un chiffre en millier
    pst = title_number - 1 #pour avoir dernier numéro liste

    if "Followers" in title_set[0]:
        title_followers = title_set[0].replace(" Followers", "") #garde juste nombre
    else:
        title_followers = f"{title_set[0]}{title_set[1]}".replace(" Followers", "")

    title_following = title_set[1].replace(" Following", "") #garde juste nombre


    if title_number >= 4: #si chiffre en millier:
        title_posts = f"{title_set[pst-1]}{title_set[pst]}".replace(" Posts", "")
    else: #sinon
        title_posts = title_set[2].replace(" Posts", "")




    print(title, ">", title_followers, ">", title_following, ">", title_posts)

【问题讨论】:

  • 它被称为节流。像 instagram 这样的网站,您需要能够使用计时器来减慢访问速度。下次使用 selenium 等来抓取此类网站。

标签: python python-requests request instagram


【解决方案1】:

instagram API 限制每个用户的 API 请求,也称为速率限制。一个用户每小时最多可以进行 200 次 API 调用。因此,如果您在一小时内进行了多达 200 次 API 调用,那么您需要等到该小时结束,它才会停止阻止您。

【讨论】:

  • 好的,但我没有使用 API,我只是直接在链接上使用 python 请求。
  • 哦,好的。如果您不使用 API,则不应使用 api 标签。
  • 你说得对,我不应该!但是你有什么想法吗?
  • 不,想法!我认为对这个问题的第一条评论可能会有所帮助!在谷歌上,它显示 - 带宽限制是互联网服务提供商故意减慢或加速互联网服务。
猜你喜欢
  • 2012-04-22
  • 1970-01-01
  • 2020-12-06
  • 1970-01-01
  • 2022-10-25
  • 2012-03-20
  • 2017-04-26
  • 2015-02-14
  • 1970-01-01
相关资源
最近更新 更多