【问题标题】:How to integrate this script with this function in Python(Instagram)如何在 Python(Instagram) 中将此脚本与此功能集成
【发布时间】:2016-12-14 06:28:28
【问题描述】:

我正在做一个小脚本,我想收集所有关于标签的“代码:”。

例如:

https://www.instagram.com/explore/tags/%s/?__a=1

下一页将是:

https://www.instagram.com/explore/tags/plebiscito/?__a=1&max_id=end_cursor

但是,我的缺点是让每个 url 都能得到我需要的东西(这是人们的 cmets 和用户名)。 所以当脚本工作时,它并不能满足我的需要。

“obtain_max_id”函数有效,得到以下 end_cursors,但我不知道如何适应它。 感谢您的帮助!

总之,我需要在“connect_main”函数中调整“obtain_max_id”函数,以提取每个 URL 所需的信息。

【问题讨论】:

  • 您的问题不清楚。您到底在寻找什么?
  • 如何完成整个过程,但是对于每个 URL,当您完成提取数据后,对以下 URL 执行相同操作(instagram.com/explore/tags/plebiscito/… 步骤如下: URL - 提取数据 Next url.. . URL - 提取数据 ....成功@Quirk
  • 我更新了我的问题,请查看。 @Quirk

标签: python instagram


【解决方案1】:

这很简单。

import requests
import json

host = "https://www.instagram.com/explore/tags/plebiscito/?__a=1"

r = requests.get(host).json()

for x in r['tag']['media']['nodes']:
   print (x['code'])

next = r['tag']['media']['page_info']['end_cursor']

while next:
   r = requests.get(host + "&max_id=" + next ).json()
   for x in r['tag']['media']['nodes']:
      print (x['code'])

   next = r['tag']['media']['page_info']['end_cursor']

【讨论】:

  • 你的 max_id 是当 r['tag']['media']['page_info']['end_cursor'] 为 None 时
【解决方案2】:

在您执行该行之后,您的data 变量(JSON 格式)中就有您想要的所有数据:

data = json.loads(finish.text)

obtain_max_id() 方法内的while 循环中。就用那个吧。

假设您的connect_main() 方法的else 块内的所有内容都有效,您可以在data 变量中的所有数据之后立即在上述while 循环中使用该代码。

【讨论】:

  • 但是问题是“obtain_max_id”函数一个接一个地拉出游标,我需要按照我说的,我需要提取的数据一个一个来做每个 URL 的 JSON @Quirk
  • 当您将值附加到 end_cursor[] 并在下一次迭代中使用 that 值来获取新的值时,您已经 这样做了页面并将其加载到data。当您从一个空的end_cursor[] 列表开始并在您访问的每个页面上添加一个光标时,当您的循环结束时,您将访问与光标对应的所有页面。
  • 我明白了,但是在提取 cmets 和用户名时应该使用另一个 While (while count
  • 为什么它会陷入无限循环?
  • 我不知道我还能做些什么来获得 end_cursors。我认为获得下一个 end_cursor 就足以将其放入列表中,并且该列表始终由以下 end_cursor 修改,但我不知道该怎么做。 @Quirk
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 2015-04-22
  • 1970-01-01
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多