如何在 python 中抓取 instagram 帐户信息
How to scrape instagram account info in python
我正在尝试在 python 中做一些非常简单的事情,但不知何故却非常困难。我想要做的就是编写一个 python 脚本来记录 Instagram 用户关注的人数及其关注者的数量。就是这样。
谁能给我指点一个好的包来做这个? 最好不要做漂亮的汤,因为这对我想做的事情来说太复杂了。我只想要
[user: example_user, followers:9019, following:217]
是否有特定于 Instagram 的 python 库?
我要抓取的帐户是 public。这对于 Twitter 来说非常简单。
感谢任何帮助。
否则你可以用那种方式访问信息(是的,我用过beautifulsoup)
from bs4 import BeautifulSoup
import urllib
external_sites_html =
urllib.urlopen('https://www.instagram.com/<instagramname>/?hl=en')
soup = BeautifulSoup(external_sites_html, 'lxml')
name = soup.find('meta', attrs={'property':'og:title'})
description = soup.find('meta', attrs={'property':'og:description'})
# name of user
nameContent = name.get('content')
# information about followers and following users
descrContent = description.get('content')
您可以从该变量中提取所需的信息。但如果关注者人数超过 100 万,有关关注者的信息将不准确。如果您需要确切的数字,您可能需要使用他们的 api.
我不知道你为什么要避免使用 BeautifulSoup,因为它实际上对这样的任务非常方便。因此,按照以下几行应该可以完成这项工作:
import requests
from bs4 import BeautifulSoup
html = requests.get('https://www.instagram.com/cristiano/') # input URL here
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]
print('User:', user)
print('Followers:', followers)
print('Following:', following)
...输出:
User: Cristiano Ronaldo (@cristiano)
Followers: 111.5m
Following: 387
当然,在用户拥有超过 100 万粉丝(或正在关注超过 100 万用户)的情况下,您需要进行一些计算才能获得实际(但被截断)的数字,这应该不会太困难.
由于您要查找的内容在页面源中可用,因此您可以结合使用请求和 BeautifulSoup 来获取它们。
试一试:
import requests
from bs4 import BeautifulSoup
html = requests.get('https://www.instagram.com/michaeljackson/')
soup = BeautifulSoup(html.text, 'lxml')
item = soup.select_one("meta[property='og:description']")
name = item.find_previous_sibling().get("content").split("•")[0]
followers = item.get("content").split(",")[0]
following = item.get("content").split(",")[1].strip()
print(f'{name}\n{followers}\n{following}')
结果:
Name :Michael Jackson
Followers :1.6m
Following :4
有一个包instagramy
pip install instagramy
from instagramy import InstagramUser
user = InstagramUser("github")
profile_pic = user.profile_pic_url
print(user.is_verified)
print(user.number_of_followers)
print(user.number_of_posts)
您可以使用 instascrape 只需几行代码就可以做到这一点(免责声明:我是这个包的作者)
pip install it using pip install insta-scrape
然后获取用户的帐户信息,try
from instascrape import Profile
google = Profile("google")
google.scrape()
这会从帐户中加载几十个数据点,您可以使用点符号访问这些数据点,例如 google.followers
、google.following
、google.is_verified
等,或者您可以获得所有数据作为 dict
和 google.to_dict()
{'csrf_token': '19DnM5UYbxusoSnbfUNGGiOr5hU91khz',
'viewer': None,
'viewer_id': None,
'country_code': 'US',
'language_code': 'en',
'locale': 'en_US',
'device_id': 'A0CFC9ED-5769-4951-94B3-F26D5724FDBD',
'browser_push_pub_key': 'BIBn3E_rWTci8Xn6P9Xj3btShT85Wdtne0LtwNUyRQ5XjFNkuTq9j4MPAVLvAFhXrUU1A9UxyxBA7YIOjqDIDHI',
'key_id': '132',
'public_key': 'a185b716b7bab1acb25e88034374819c0482257a4e240736215af2253f255d61',
'version': '10',
'is_dev': False,
'rollout_hash': '7b740aa85a82',
'bundle_variant': 'metro',
'frontend_dev': 'prod',
'logging_page_id': 'profilePage_1067259270',
'show_suggested_profiles': False,
'show_follow_dialog': False,
'biography': 'Google unfiltered—sometimes with filters.',
'blocked_by_viewer': False,
'business_email': '',
'restricted_by_viewer': None,
'country_block': False,
'external_url': 'https://linkin.bio/google',
'external_url_linkshimmed': 'https://l.instagram.com/?u=https%3A%2F%2Flinkin.bio%2Fgoogle&e=ATOMCBcW4YjsNBxlDyAETnOiWt8zHrGTW0VJIufW-ROhSYM5lm2p-JNT060OLDBmMFuoszepQpW0cfEf&s=1',
'followers': 12262801,
'followed_by_viewer': False,
'following': 30,
'follows_viewer': False,
'full_name': 'Google',
'has_ar_effects': False,
'has_clips': True,
'has_guides': False,
'has_channel': False,
'has_blocked_viewer': False,
'highlight_reel_count': 6,
'has_requested_viewer': False,
'id': '1067259270',
'is_business_account': True,
'is_joined_recently': False,
'business_category_name': 'Business & Utility Services',
'overall_category_name': None,
'category_enum': 'INTERNET_COMPANY',
'is_private': False,
'is_verified': True,
'mutual_followers': 0,
'profile_pic_url': 'https://scontent-lga3-1.cdninstagram.com/v/t51.2885-19/s150x150/119515245_239175997499686_2853342285794408974_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_ohc=_vp0OGMhUrEAX9mEskb&oh=242d04421b13f2545952203069b164b6&oe=5FC05FDB',
'profile_pic_url_hd': 'https://scontent-lga3-1.cdninstagram.com/v/t51.2885-19/s320x320/119515245_239175997499686_2853342285794408974_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_ohc=_vp0OGMhUrEAX9mEskb&oh=604348546412db230d638292b39f7abe&oe=5FC2E223',
'requested_by_viewer': False,
'username': 'google',
'connected_fb_page': None,
'posts': 1416}
如果您真的只需要几个数据点,您可以将它们的名称作为键显式传递给 Post.load
from instascrape import Profile
google = Profile("google")
google.scrape(keys=['followers', 'following'])
和google.to_dict()
将因此给我们
{'followers': 12262807, 'following': 30}
import requests
username = "cristiano"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
user_info = requests.get('https://instagram.com/%s/?__a=1'%username, headers = headers)
print (user_info.json())
我正在尝试在 python 中做一些非常简单的事情,但不知何故却非常困难。我想要做的就是编写一个 python 脚本来记录 Instagram 用户关注的人数及其关注者的数量。就是这样。
谁能给我指点一个好的包来做这个? 最好不要做漂亮的汤,因为这对我想做的事情来说太复杂了。我只想要
[user: example_user, followers:9019, following:217]
是否有特定于 Instagram 的 python 库?
我要抓取的帐户是 public。这对于 Twitter 来说非常简单。
感谢任何帮助。
否则你可以用那种方式访问信息(是的,我用过beautifulsoup)
from bs4 import BeautifulSoup
import urllib
external_sites_html =
urllib.urlopen('https://www.instagram.com/<instagramname>/?hl=en')
soup = BeautifulSoup(external_sites_html, 'lxml')
name = soup.find('meta', attrs={'property':'og:title'})
description = soup.find('meta', attrs={'property':'og:description'})
# name of user
nameContent = name.get('content')
# information about followers and following users
descrContent = description.get('content')
您可以从该变量中提取所需的信息。但如果关注者人数超过 100 万,有关关注者的信息将不准确。如果您需要确切的数字,您可能需要使用他们的 api.
我不知道你为什么要避免使用 BeautifulSoup,因为它实际上对这样的任务非常方便。因此,按照以下几行应该可以完成这项工作:
import requests
from bs4 import BeautifulSoup
html = requests.get('https://www.instagram.com/cristiano/') # input URL here
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]
print('User:', user)
print('Followers:', followers)
print('Following:', following)
...输出:
User: Cristiano Ronaldo (@cristiano)
Followers: 111.5m
Following: 387
当然,在用户拥有超过 100 万粉丝(或正在关注超过 100 万用户)的情况下,您需要进行一些计算才能获得实际(但被截断)的数字,这应该不会太困难.
由于您要查找的内容在页面源中可用,因此您可以结合使用请求和 BeautifulSoup 来获取它们。
试一试:
import requests
from bs4 import BeautifulSoup
html = requests.get('https://www.instagram.com/michaeljackson/')
soup = BeautifulSoup(html.text, 'lxml')
item = soup.select_one("meta[property='og:description']")
name = item.find_previous_sibling().get("content").split("•")[0]
followers = item.get("content").split(",")[0]
following = item.get("content").split(",")[1].strip()
print(f'{name}\n{followers}\n{following}')
结果:
Name :Michael Jackson
Followers :1.6m
Following :4
有一个包instagramy
pip install instagramy
from instagramy import InstagramUser
user = InstagramUser("github")
profile_pic = user.profile_pic_url
print(user.is_verified)
print(user.number_of_followers)
print(user.number_of_posts)
您可以使用 instascrape 只需几行代码就可以做到这一点(免责声明:我是这个包的作者)
pip install it using pip install insta-scrape
然后获取用户的帐户信息,try
from instascrape import Profile
google = Profile("google")
google.scrape()
这会从帐户中加载几十个数据点,您可以使用点符号访问这些数据点,例如 google.followers
、google.following
、google.is_verified
等,或者您可以获得所有数据作为 dict
和 google.to_dict()
{'csrf_token': '19DnM5UYbxusoSnbfUNGGiOr5hU91khz',
'viewer': None,
'viewer_id': None,
'country_code': 'US',
'language_code': 'en',
'locale': 'en_US',
'device_id': 'A0CFC9ED-5769-4951-94B3-F26D5724FDBD',
'browser_push_pub_key': 'BIBn3E_rWTci8Xn6P9Xj3btShT85Wdtne0LtwNUyRQ5XjFNkuTq9j4MPAVLvAFhXrUU1A9UxyxBA7YIOjqDIDHI',
'key_id': '132',
'public_key': 'a185b716b7bab1acb25e88034374819c0482257a4e240736215af2253f255d61',
'version': '10',
'is_dev': False,
'rollout_hash': '7b740aa85a82',
'bundle_variant': 'metro',
'frontend_dev': 'prod',
'logging_page_id': 'profilePage_1067259270',
'show_suggested_profiles': False,
'show_follow_dialog': False,
'biography': 'Google unfiltered—sometimes with filters.',
'blocked_by_viewer': False,
'business_email': '',
'restricted_by_viewer': None,
'country_block': False,
'external_url': 'https://linkin.bio/google',
'external_url_linkshimmed': 'https://l.instagram.com/?u=https%3A%2F%2Flinkin.bio%2Fgoogle&e=ATOMCBcW4YjsNBxlDyAETnOiWt8zHrGTW0VJIufW-ROhSYM5lm2p-JNT060OLDBmMFuoszepQpW0cfEf&s=1',
'followers': 12262801,
'followed_by_viewer': False,
'following': 30,
'follows_viewer': False,
'full_name': 'Google',
'has_ar_effects': False,
'has_clips': True,
'has_guides': False,
'has_channel': False,
'has_blocked_viewer': False,
'highlight_reel_count': 6,
'has_requested_viewer': False,
'id': '1067259270',
'is_business_account': True,
'is_joined_recently': False,
'business_category_name': 'Business & Utility Services',
'overall_category_name': None,
'category_enum': 'INTERNET_COMPANY',
'is_private': False,
'is_verified': True,
'mutual_followers': 0,
'profile_pic_url': 'https://scontent-lga3-1.cdninstagram.com/v/t51.2885-19/s150x150/119515245_239175997499686_2853342285794408974_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_ohc=_vp0OGMhUrEAX9mEskb&oh=242d04421b13f2545952203069b164b6&oe=5FC05FDB',
'profile_pic_url_hd': 'https://scontent-lga3-1.cdninstagram.com/v/t51.2885-19/s320x320/119515245_239175997499686_2853342285794408974_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_ohc=_vp0OGMhUrEAX9mEskb&oh=604348546412db230d638292b39f7abe&oe=5FC2E223',
'requested_by_viewer': False,
'username': 'google',
'connected_fb_page': None,
'posts': 1416}
如果您真的只需要几个数据点,您可以将它们的名称作为键显式传递给 Post.load
from instascrape import Profile
google = Profile("google")
google.scrape(keys=['followers', 'following'])
和google.to_dict()
将因此给我们
{'followers': 12262807, 'following': 30}
import requests
username = "cristiano"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
user_info = requests.get('https://instagram.com/%s/?__a=1'%username, headers = headers)
print (user_info.json())