Why getting different data in browser developer tools vs BeautifulSoap / Postman?

Viewed 133

I want to scrap data from this web page

I want to get all the blogs...which are under result tag (<div class="results">)

In browser tools there it is showing under result tag there are 10 snippets... enter image description here

But using Beautifulsoap I am getting

<div class="results">
</div>

and in postman getting same thing..

This is the way I am doing..

topicuri = "\"
r = s.get(topicuri)
soup = BeautifulSoup(r.text, 'html.parser')
pages = soup.find('div', {'class': 'results'})
print(pages)
3 Answers

The website is using Javascript to display the snippets. BeautifulSoup does not execute Javascript, while the browser does. You will probably want to use the Chromium engine in Python in order to web-scrape Javascript-based content.

You also can get data from api calls json response

import requests
import json 
body= "vodafone"
headers= {
    'content-type': 'application/json'
   }

api_url = "https://search.donanimhaber.com/api/search/portal/?q=vodafone&p=3&devicetype=browsermobile&order=date_desc&in=all&contenttype=all&wordtype=both&daterange=all"

jsonData = requests.post(api_url, data=json.dumps(body), headers=headers).json()

for item in jsonData['contents']:
    categoryName=item['categoryName']
    print(categoryName)

Output:

Operatörler - Kurumsal Haberler
Operatörler - Kurumsal Haberler
Operatörler - Kurumsal Haberler
Mobil Aksesuarlar
Operatörler - Kurumsal Haberler
Kripto Para
Sinema ve Dizi
Mobil Oyunlar
Operatörler - Kurumsal Haberler
Operatörler - Kurumsal Haberler

As mentioned requests could not render JavaScript but there are two alternatives:

  • Use requests and perform a post request on your url
  • Use selenium to get the rendered page_source as you would expect it.

Example

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup

url = 'https://search.donanimhaber.com/portal?q=vodafone&p=3&devicetype=browsermobile&order=date_desc&in=all&contenttype=all&wordtype=both&range=all'
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get(url)

wait.until(EC.presence_of_all_elements_located((By.XPATH, './/div[@class="results"]/div[@class="snippet"]')))

content = driver.page_source
soup = BeautifulSoup(content,"html.parser")

pages = soup.find_all('div', {'class': 'snippet'})

for p in pages:
    print(p.h2.text.strip())

Output

Vodafone'dan dijital sağlık projelerine ücretsiz 5G desteği
Vodafone'un son 15 yılda Türkiye ekonomisine katkısı açıklandı
"Yarını Kodlayanlar" projesinde gençler afet sorunlarına çözümler üretti
Küresel akıllı saat pazarı yılın ilk çeyreğinde yüzde 35 büyüdü
Vodafone Türkiye'nin ilk çeyrek sonuçları açıklandı: Servis gelirlerinde yüzde 19 artış
Netflix'e yeni eklenen dizi ve filmleri takip edebileceğiniz site
Sony ve SinemaTV anlaştı! Spider-Man, Venom 2 ve daha fazlası TV'de ilk kez SinemaTV'de yayınlanacak
Vodafone ve Riot Games, Türkiye'nin ilk 5G Wild Rift turnuvasını duyurdu
Türkiye'de kaç kişi numara taşıma ile operatör değiştirdi?
Turkcell'in Ramazan'a özel Salla Kazan kampanyası başladı
Related