2
votes

I want to scrape a website's amount of likes. Using BeautifulSoup, this is what I got so far:

user = 'LazadaMalaysia'

url = 'https://www.facebook.com/'+ user
response = requests.get(url)
soup = BeautifulSoup(response.content,'lxml')
f = soup.find('div', attrs={'class': '_4bl9'})

The output I receive for f is as follows:

<div class="_4bl9 _3bcp"><div aria-keyshortcuts="Alt+/" aria-label="Pembantu Navigasi" class="_6a _608n" id="u_0_8" role="menubar"><div class="_6a uiPopover" id="u_0_9"><a aria-expanded="false" aria-haspopup="true" class="_42ft _4jy0 _55pi _2agf _4o_4 _63xb _p _4jy3 _517h _51sy" href="#" id="u_0_a" rel="toggle" role="button" style="max-width:200px;"><span class="_55pe">Bahagian-bahagian pada halaman ini</span><span class="_4o_3 _3-99"><i class="img sp_m7lN5cdLBIi sx_d3bfaf"></i></span></a></div><div class="_6a _3bcs"></div><div class="_6a mrm uiPopover" id="u_0_b"><a aria-expanded="false" aria-haspopup="true" class="_42ft _4jy0 _55pi _2agf _4o_4 _3_s2 _63xb _p _4jy3 _4jy1 selected _51sy" href="#" id="u_0_c" rel="toggle" role="button" style="max-width:200px;" tabindex="-1"><span class="_55pe">Bantuan Kebolehcapaian</span><span class="_4o_3 _3-99"><i class="img sp_m7lN5cdLBIi sx_0a4c0e"></i></span></a></div></div></div>

I used the code from this link: How do I scrape the about section of a Facebook page?

Unfortunately, it does not work and I cannot wrap my head around why this is the case. Here is the part I want to scrape:

enter image description here

2
It looks like there are multiple divs of class _4b19, since you are using find(), it's only to give you the first instance of that class from the page. The point I'm trying to get at is that the specific div you want with the likes could be further down the page and you just aren't catching it yet. Using find_all() will give you a list of all classes _4b19, try looking through that list to see if you are even picking up the likes or you may need to rework the parameters of your find() - David Owens
Had the same thought, unfortunately find_all() returns an empty list and is thus not able to filter it out properly as well. Trying to work with the "people like this" string and re.search as it is unique within the page source. - calicationoflife
Give soup.find_all("div", string="people like this") a shot? - David Owens
still returns an empty list. =/ - calicationoflife
Don’t scrape Facebook, that is not allowed. You can simply use the API for this: developers.facebook.com/tools/… - CBroe

2 Answers

5
votes

The likes are in span tag inside class "_4-u3 _5sqi _5sqk".Here is the code that will extract likes.

import requests
from bs4 import BeautifulSoup
user = 'LazadaMalaysia'
url = 'https://www.facebook.com/'+ user
response = requests.get(url)
soup = BeautifulSoup(response.content,'lxml')
f = soup.find('div', attrs={'class': '_4-u3 _5sqi _5sqk'})
likes=f.find('span',attrs={'class':'_52id _50f5 _50f7'}) #finding span tag inside class
print(likes.text)

I hope I have solved your problem.

1
votes

I find the following easy to go with. It should fetch the number of likes and follows.

import re
import requests
from bs4 import BeautifulSoup

def get_info(user,url):
    response = requests.get(f'{url}{user}')
    soup = BeautifulSoup(response.text,'lxml')
    like = soup.find("div",text=re.compile('people like this')).text
    follow = soup.find("div",text=re.compile('people follow this')).text
    print(f'likes: {like}\nfollows: {follow}\n')

if __name__ == '__main__':
    url = "https://www.facebook.com/"
    users = ['LazadaMalaysia','ronaldo','rihanna']
    [get_info(user,url) for user in users]