Read data from reviews on the Daraz website
Question:
This code perfectly runs in other URL like getting a product or paragraph. But cannot able to read reviews.
import csv
from lib2to3.pgen2 import driver
from operator import itemgetter
from re import template
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome('H:/PythonMechineLearnning/WebScrape/chromedriver_win32/chromedriver.exe')
url = 'https://www.daraz.com.bd/products/1-livingtex-_-i183964360-s1169754856.html?spm=a2a0e.searchlist.list.2.6a801fbaMbkK8g&search=1'
driver.get(url)
print(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')
result = soup.find_all('div', {'class': 'content'})
print(result[0])
Error
IndexError: list index out of range
cannot able to read data
Answers:
Everything is in the HTML
. There’s an embedded JSON
there so you don’t really need chromdriver
nor bs4
.
You can get the reviews with plain requests
.
Here’s how:
import json
import re
import requests
url = "https://www.daraz.com.bd/products/1-livingtex-_-i183964360-s1169754856.html?spm=a2a0e.searchlist.list.2.6a801fbaMbkK8g&search=1"
data = (
json.loads(
re.search(
r"app.run((.*));",
requests.get(url).text,
re.MULTILINE,
).group(1)
)
)
reviews = data["data"]["root"]["fields"]["review"]
print(f'{reviews["ratings"]}n{"-" * 80}')
for review in reviews["reviews"]:
print(f'{review["reviewer"]} -> {review["reviewContent"]}n')
Output:
{'average': 3.7, 'rateCount': 83, 'reviewCount': 68, 'scores': [47, 7, 7, 4, 18]}
--------------------------------------------------------------------------------
Asif -> ৪৯ টাকায় কেনা হয়েছে। খুবই ভালো মানের পন্য। চাইলে সবাই নিতে পারেন। ডেলিভারি অনেক দ্রুত ছিল, ধন্যবাদ সেলারকে।
Mushfiqur R. -> ছবির প্রোডাক্ট আর এই প্রোডাক্টটা কি সেম? I don’t think so. বাট জিনিস টা পরে কমফোর্টেবল আছে।
1***3 -> I've ordered Blue Color Boxer XL size from Livingtex but they have sent me wrong color product (Black). Earlier Livingtex done this type of mistake with me. Is this a professionalism? How can they do this type of mistake repeatedly? This is So Sad.
P***. -> সাইজ বিবরণ অনুযায়ী ঠিক ছিল কিন্তু কাপড়ের মান ভালো না। Just দুই দিন পরার পরই পরতে গিয়ে পায়ে লেগে ছিঁড়ে যায়। কাপড় খুবই নরম এবং খারাপ। কোমরের রাবারও বেশ লুজ।
Alamin K. -> ceyesi M dise L no problem product return disi product amk a diya diya hoyse abr cashbake o paye gesi full taka thanks daraz
This code perfectly runs in other URL like getting a product or paragraph. But cannot able to read reviews.
import csv
from lib2to3.pgen2 import driver
from operator import itemgetter
from re import template
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome('H:/PythonMechineLearnning/WebScrape/chromedriver_win32/chromedriver.exe')
url = 'https://www.daraz.com.bd/products/1-livingtex-_-i183964360-s1169754856.html?spm=a2a0e.searchlist.list.2.6a801fbaMbkK8g&search=1'
driver.get(url)
print(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')
result = soup.find_all('div', {'class': 'content'})
print(result[0])
Error
IndexError: list index out of range
cannot able to read data
Everything is in the HTML
. There’s an embedded JSON
there so you don’t really need chromdriver
nor bs4
.
You can get the reviews with plain requests
.
Here’s how:
import json
import re
import requests
url = "https://www.daraz.com.bd/products/1-livingtex-_-i183964360-s1169754856.html?spm=a2a0e.searchlist.list.2.6a801fbaMbkK8g&search=1"
data = (
json.loads(
re.search(
r"app.run((.*));",
requests.get(url).text,
re.MULTILINE,
).group(1)
)
)
reviews = data["data"]["root"]["fields"]["review"]
print(f'{reviews["ratings"]}n{"-" * 80}')
for review in reviews["reviews"]:
print(f'{review["reviewer"]} -> {review["reviewContent"]}n')
Output:
{'average': 3.7, 'rateCount': 83, 'reviewCount': 68, 'scores': [47, 7, 7, 4, 18]}
--------------------------------------------------------------------------------
Asif -> ৪৯ টাকায় কেনা হয়েছে। খুবই ভালো মানের পন্য। চাইলে সবাই নিতে পারেন। ডেলিভারি অনেক দ্রুত ছিল, ধন্যবাদ সেলারকে।
Mushfiqur R. -> ছবির প্রোডাক্ট আর এই প্রোডাক্টটা কি সেম? I don’t think so. বাট জিনিস টা পরে কমফোর্টেবল আছে।
1***3 -> I've ordered Blue Color Boxer XL size from Livingtex but they have sent me wrong color product (Black). Earlier Livingtex done this type of mistake with me. Is this a professionalism? How can they do this type of mistake repeatedly? This is So Sad.
P***. -> সাইজ বিবরণ অনুযায়ী ঠিক ছিল কিন্তু কাপড়ের মান ভালো না। Just দুই দিন পরার পরই পরতে গিয়ে পায়ে লেগে ছিঁড়ে যায়। কাপড় খুবই নরম এবং খারাপ। কোমরের রাবারও বেশ লুজ।
Alamin K. -> ceyesi M dise L no problem product return disi product amk a diya diya hoyse abr cashbake o paye gesi full taka thanks daraz