问题
How would I go about gathering this kind of data from Instagram for a web scraping project, I tried myself to get it using beautifulsoup and requests true parsing the whole page: but it doesn't work
import requests
from bs4 import BeautifulSoup
usrs=[]
soup=BeautifulSoup(requests.get("https://www.instagram.com/p/Bziq7f2C-jM/").content,'html.parser')
elem1=soup.find_all('div',class_="EtaWk")
#elem1 contains all the usernames within it
if elem1:
elem2=elem1.find('ul',class_="XQXOT")
if elem2:
xelems=elem2.findAll('ul',class_="Mr508")
for i in range(len(xelems)):
lis=xelems[i].find('a',class_="FPmhX notranslate TlrDj",title=True)
usrs.append(a["title"])
回答1:
If it is not a requirement for you to use Beautifulsoup and parse the HTML response yourself, there is Instaloader, a Python library that allows to access Instagram very easily. After doing pip install instaloader
to install it, you can do
import instaloader
L = instaloader.Instaloader()
Post = instaloader.Post.from_shortcode(L.context, 'Bziq7f2C-jM')
Then, Post.get_likes()
returns an Iterator over the Profiles that have liked the Post, so to print all the usernames, you can do
for like in Post.get_likes():
print(like.username)
Besides being an easy solution, Instaloader has also the advantage that it handles rate limiting automatically, and that it supports handling login and accessing posts of private profiles.
来源:https://stackoverflow.com/questions/56916810/how-to-web-scrape-users-who-liked-an-instagram-picture