【问题标题】:Get top images of hashtag in Instagram without API无需 API 即可在 Instagram 中获取主题标签的热门图片
【发布时间】:2020-07-05 02:51:34
【问题描述】:

这是我用来在没有 API.no client_id 或访问令牌的情况下获取主题标签顶部图像的代码。它在我的本地主机中运行良好。但是当我把它放在在线主机(pythoneverywhere)上时。它没有!

def hashtagTracker(request):

    if request.GET.get('num1'):
        hashtag = request.GET['num1']
        # print("\033[1m" + "Scraping/analyzing posts for " + hashtag + "..." + "\033[0m")
        page = requests.get("https://www.instagram.com/explore/tags/" + hashtag[1:])
        posts = json.loads(page.text[page.text.find("window._sharedData") + 21: page.text.find("};</script>") + 1])
        postCount = posts["entry_data"]["TagPage"][0]["graphql"]["hashtag"]["edge_hashtag_to_media"]["count"]
       

似乎在在线版本中,请求被定向到登录门户。而这在 localhost 版本中没有发生。谁能帮我解决这个问题并让它在在线主机上运行并节省我的一周?

谢谢你,对不起我的英语:)

【问题讨论】:

  • 将html保存为文件并分析。我推荐使用BeautifulSoup 来解析html

标签: python json instagram


【解决方案1】:

您只需登录即可使用请求模块绕过登录门户。

import requests
url = 'https://www.website.com/login'
username = 'slim shady'
password = 'password'
requests.Session().get(url)
login_data = dict(USERNAME=username, PASSWORD=password)#the capitalised tags here have to be the ones from the website, you can use chrome console to find the right tags to use
requests.Session().post(url, data=login_data)

我认为在您的代码被登录页面击中时运行此程序应该可以正常工作,但是可能会出现一些问题。尝试单步执行现有代码并检查变量以查看登录门户出现的位置。

【讨论】:

    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 2014-09-17
    • 2021-01-06
    相关资源
    最近更新 更多