如何使用python获得Instagram粉丝的真实数量而不是k

2024-10-01 15:41:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一个问题,当我抓取Instagram关注者的数量而不是实际数量时,我得到了“k”的缩写。在

import requests, os, time, sys
from bs4 import BeautifulSoup
import pandas as pd

def insta_info(account_name):
    html = requests.get('https://www.instagram.com/%s/'%(account_name)) 
    soup = BeautifulSoup(html.text, 'lxml')
    data = soup.find_all('meta', attrs={'property':'og:description'})
    text = data[0].get('content').split()
    user = '%s %s %s' % (text[-3], text[-2], text[-1])
    followers = text[0]
    following = text[2]
    lst = []
    lst.append(followers)
    lst.append(following)
    return lst

kellz = insta_info(kellz_ocho)
print(kellz)

这将返回:

^{pr2}$

当我想要它回来的时候:

[14241, 608]

有没有办法做到这一点?我没有写上面的代码,而是在网上找到并实现了它。因此,我不确定它到底是如何工作的。请注意,我要收集的帐户是公开的。在

多谢了!在


Tags: textnameimportinfodataget数量html
3条回答

这应该行得通。基本上,附加代码检查“k”,如果有“k”,则将剩余部分乘以1000

import requests, os, time, sys
from bs4 import BeautifulSoup
import pandas as pd

def insta_info(account_name):
    html = requests.get('https://www.instagram.com/%s/'%(account_name)) 
    soup = BeautifulSoup(html.text, 'lxml')
    data = soup.find_all('meta', attrs={'property':'og:description'})
    text = data[0].get('content').split()
    user = '%s %s %s' % (text[-3], text[-2], text[-1])
    followers = text[0]
    if followers[-1] == 'K':
        followers = int(float(followers[:-1].encode('UTF-8')) * 1000)
    else:
        followers = int(float(followers.encode('UTF-8')))
    following = text[2]
    if following[-1] == 'K':
        following = int(float(following[:-1].encode('UTF-8')) * 1000)
    else:
        following = int(float(following.encode('UTF-8')))
    lst = []
    lst.append(followers)
    lst.append(following)
    return lst

kellz = insta_info(kellz_ocho)
print(kellz)

为了得到您想要的东西,您需要将selenium与BeautifulSoup结合使用,因为在page source中,meta标记中没有您要查找的内容;相反,唯一可用的是您已经得到的东西。试试这个:

from bs4 import BeautifulSoup ; from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.instagram.com/kellz_ocho/")
soup = BeautifulSoup(driver.page_source,"html.parser")
driver.quit()

for title in soup.select("._h9luf"):   
    posts = title.select("._fd86t")[0].text
    follower = title.select("._fd86t")[1]['title']
    following = title.select("._fd86t")[2].text
    print("Posts: {}\nFollower: {}\nFollowing: {}".format(posts,follower,following))

结果:

^{pr2}$

顺便说一句,跟随者的状态已经改变了。在

你给出的代码绝对不是正确的方法。请不要用它。在

从这个链接可以看到:https://www.instagram.com/developer/endpoints/users/获取用户信息非常简单。您甚至可以从这里获取访问令牌:http://instagram.pixelunion.net/如果您不想编写代码进行身份验证。在

相关问题 更多 >

    热门问题