网络抓取 Instagram 关注者数量 BeautifulSoup
Webscraping Instagram follower count BeautifulSoup
我刚刚开始学习如何使用 BeautifulSoup 进行网络抓取,并想编写一个简单的程序来获取给定 Instagram 页面的关注者数量.我目前有以下脚本(从另一个问答线程中提取):
import requests
from bs4 import BeautifulSoup
user = "espn"
url = 'https://www.instagram.com/'+ user
r = requests.get(url)
soup = BeautifulSoup(r.content)
followers = soup.find('meta', {'name': 'description'})['content']
follower_count = followers.split('Followers')[0]
print(follower_count)
# 10.7m
我 运行 遇到的问题是我想获得一个更精确的数字,当您将鼠标悬停在 Instagram 页面上的关注者数量(例如 10,770,816)上时,您可以看到该数字。
不幸的是,我无法弄清楚如何使用 BeautifulSoup 执行此操作。我想在没有 API 的情况下执行此操作,因为我将其与代码结合使用以跟踪其他社交媒体平台。有什么建议吗?
虽然这并不是一个真正的关于编程的一般问题,但您应该发现确切的关注者计数是包含格式化关注者计数的 span
元素的 title
属性。可以查询这个属性.
执行此操作的最简单方法是将页面 html 转储到文本编辑器中,然后通过文本搜索此人拥有的确切关注者数量。然后您可以将包含数字的元素置零。
使用 API 是最简单的方法,但我也发现了一个非常 hacky 的方法:
import requests
username = "espn"
url = 'https://www.instagram.com/' + username
r = requests.get(url).text
start = '"edge_followed_by":{"count":'
end = '},"followed_by_viewer"'
followers= r[r.find(start)+len(start):r.rfind(end)]
start = '"edge_follow":{"count":'
end = '},"follows_viewer"'
following= r[r.find(start)+len(start):r.rfind(end)]
print(followers, following)
如果您查看请求给出的响应,有一行 Javascript 包含真实的关注者数量:
...edge_followed_by":{"count":10770969},"followed_by_viewer":{
...
所以我只是通过查找前后的子串来提取数字。
Instagram 始终使用 JSON 数据进行响应,这使其成为从 JSON 获取元数据通常更清晰的选项,而不是使用 BeautifulSoup 解析 HTML 响应。鉴于使用 BeatifulSoup 不是一个限制,至少有两个干净的选项来获取 Instagram 个人资料的关注者数量:
获取个人资料页面,搜索JSON并解析:
import json
import re
import requests
response = requests.get('https://www.instagram.com/' + PROFILE)
json_match = re.search(r'window\._sharedData = (.*);</script>', response.text)
profile_json = json.loads(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user']
print(profile_json['edge_followed_by']['count'])
然后,profile_json 变量包含个人资料的元数据,而不仅仅是关注者数量。
使用库,将 Instagram 响应的更改留给上游的问题。有Instaloader,可以这样使用:
from instaloader import Instaloader, Profile
L = Instaloader()
profile = Profile.from_username(L.context, PROFILE)
print(profile.followers)
它还支持登录,也允许访问私人资料。
(免责声明:我正在编写此工具)
无论哪种方式,您都可以获得一个包含配置文件元数据的结构,而无需对 html 响应做一些奇怪的事情。
这是我的方法(html 源代码有一个 json 对象,其中包含配置文件的所有数据)
import json
import urllib.request, urllib.parse
from bs4 import BeautifulSoup
req = urllib.request.Request(myurl)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36')
html = urllib.request.urlopen(req).read()
response = BeautifulSoup(html, 'html.parser')
jsonObject = response.select("body > script:nth-of-type(1)")[0].text.replace('window._sharedData =','').replace(';','')
data = json.loads(jsonObject)
following = data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_follow']['count']
followed = data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_followed_by']['count']
posts = data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['count']
username = data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][0]['node']['owner']['username']
我刚刚开始学习如何使用 BeautifulSoup 进行网络抓取,并想编写一个简单的程序来获取给定 Instagram 页面的关注者数量.我目前有以下脚本(从另一个问答线程中提取):
import requests
from bs4 import BeautifulSoup
user = "espn"
url = 'https://www.instagram.com/'+ user
r = requests.get(url)
soup = BeautifulSoup(r.content)
followers = soup.find('meta', {'name': 'description'})['content']
follower_count = followers.split('Followers')[0]
print(follower_count)
# 10.7m
我 运行 遇到的问题是我想获得一个更精确的数字,当您将鼠标悬停在 Instagram 页面上的关注者数量(例如 10,770,816)上时,您可以看到该数字。
不幸的是,我无法弄清楚如何使用 BeautifulSoup 执行此操作。我想在没有 API 的情况下执行此操作,因为我将其与代码结合使用以跟踪其他社交媒体平台。有什么建议吗?
虽然这并不是一个真正的关于编程的一般问题,但您应该发现确切的关注者计数是包含格式化关注者计数的 span
元素的 title
属性。可以查询这个属性.
执行此操作的最简单方法是将页面 html 转储到文本编辑器中,然后通过文本搜索此人拥有的确切关注者数量。然后您可以将包含数字的元素置零。
使用 API 是最简单的方法,但我也发现了一个非常 hacky 的方法:
import requests
username = "espn"
url = 'https://www.instagram.com/' + username
r = requests.get(url).text
start = '"edge_followed_by":{"count":'
end = '},"followed_by_viewer"'
followers= r[r.find(start)+len(start):r.rfind(end)]
start = '"edge_follow":{"count":'
end = '},"follows_viewer"'
following= r[r.find(start)+len(start):r.rfind(end)]
print(followers, following)
如果您查看请求给出的响应,有一行 Javascript 包含真实的关注者数量:
...edge_followed_by":{"count":10770969},"followed_by_viewer":{
...
所以我只是通过查找前后的子串来提取数字。
Instagram 始终使用 JSON 数据进行响应,这使其成为从 JSON 获取元数据通常更清晰的选项,而不是使用 BeautifulSoup 解析 HTML 响应。鉴于使用 BeatifulSoup 不是一个限制,至少有两个干净的选项来获取 Instagram 个人资料的关注者数量:
获取个人资料页面,搜索JSON并解析:
import json import re import requests response = requests.get('https://www.instagram.com/' + PROFILE) json_match = re.search(r'window\._sharedData = (.*);</script>', response.text) profile_json = json.loads(json_match.group(1))['entry_data']['ProfilePage'][0]['graphql']['user'] print(profile_json['edge_followed_by']['count'])
然后,profile_json 变量包含个人资料的元数据,而不仅仅是关注者数量。
使用库,将 Instagram 响应的更改留给上游的问题。有Instaloader,可以这样使用:
from instaloader import Instaloader, Profile L = Instaloader() profile = Profile.from_username(L.context, PROFILE) print(profile.followers)
它还支持登录,也允许访问私人资料。
(免责声明:我正在编写此工具)
无论哪种方式,您都可以获得一个包含配置文件元数据的结构,而无需对 html 响应做一些奇怪的事情。
这是我的方法(html 源代码有一个 json 对象,其中包含配置文件的所有数据)
import json
import urllib.request, urllib.parse
from bs4 import BeautifulSoup
req = urllib.request.Request(myurl)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36')
html = urllib.request.urlopen(req).read()
response = BeautifulSoup(html, 'html.parser')
jsonObject = response.select("body > script:nth-of-type(1)")[0].text.replace('window._sharedData =','').replace(';','')
data = json.loads(jsonObject)
following = data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_follow']['count']
followed = data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_followed_by']['count']
posts = data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['count']
username = data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][0]['node']['owner']['username']