如何从网站上抓取评论"flipkart.com"



我不明白选择哪个类,我尝试了不同的选择器类,但它返回空列表

我尝试了以下代码。

import requests as req
from bs4 import BeautifulSoup as bs
url = 'https://www.flipkart.com/nokia-6-1-plus-black-64-gb/product-reviews/itmf8r36g9gfpafg?pid=MOBF8FCFB9KWUTVQ'
page = req.get(url)
rev = soup.find_all(class_ = "_2xg6Ul")

我想扩展评论并将其存储在文本文件中以供以后使用

你永远不会在你说你尝试过的代码中定义soup

但是你不需要使用Selenium,因为评论在<script>标签内。唯一的问题是您必须遍历每个页面才能获得所有评论,但是如果您要使用 Selenium,无论如何您都需要这样做(因为它每页只有 10 个......在本例中,有 1976 页(。但这会让你得到评论:

注意:我只做了5页。如果你想做所有 1900+,你需要注释掉我硬编码的行。

import requests as req
from bs4 import BeautifulSoup as bs
import json
import math
# Get Total Pages
url = 'https://www.flipkart.com/nokia-6-1-plus-black-64-gb/product-reviews/itmf8r36g9gfpafg?pid=MOBF8FCFB9KWUTVQ'
page = req.get(url)
soup = bs(page.text, 'html.parser')

scripts = soup.find_all('script')
for script in scripts:
    if 'window.__INITIAL_STATE__ = ' in script.text:
        script_str = script.text
        jsonStr = script_str.split('window.__INITIAL_STATE__ = ')[1]
        jsonStr = jsonStr.rsplit(';',1)[0]
        jsonObj = json.loads(jsonStr)
        total_pages = math.ceil(jsonObj['ratingsAndReviews']['reviewsData']['totalCount'] / 10)


total_pages=5  # <------ remove this to get all pages, or set you page limit
for page in range(1,total_pages+1):
    page_url = url + '&page=%s' %page
    print ('Page %s' %page)
    page = req.get(page_url)
    soup = bs(page.text, 'html.parser')

    scripts = soup.find_all('script')
    for script in scripts:
        if 'window.__INITIAL_STATE__ = ' in script.text:
            script_str = script.text
            jsonStr = script_str.split('window.__INITIAL_STATE__ = ')[1]
            jsonStr = jsonStr.rsplit(';',1)[0]
            jsonObj = json.loads(jsonStr)

    for each in jsonObj['ratingsAndReviews']['reviewsData']['reviewsData']['nonAspectReview']:
        print (each['value']['text'],'n')

输出:

Page 1
looking for buy just go for it 👍
Today I received Nokia 6.1 plus mobile phone and this phone is amazing performance and build quality I am loving it 😍🙋👌👍 
Ordered Nokia 6.1plus(Blue)variant on 30th August and received it on 1st Sept. After using it for 22days,i m writing down its review.
PROS:
1)Android One- out of the box 8.1 oreo with Andoid P and Andoid O in the offing. Smooth and fluidic OS with no bloatware and unnecessary apps and design and regular security updates.
2)Design-Absolutely premium. Glass back with stunning looks as well as compact. 2.5 D curved glass with seamless look. Feels good to hand and can be used with one hand. At this price segment its a jackpot.
3)Display- 5.8 inch Full HD + display(443ppi) with 19:9 notch ratio. lifelike visuals, vivid resolution coupled with corning gorrila glass.
4)Performance - it sports a Qualcomm 636 processor and 4gb Ram.Honestly performance is mindblowing. Multitasking is smooth. Gaming performance is absolutely brilliant.. Asphalt 9 runs like dream as well as other games like PUBG etc.
5)Storage-51gb available to use. Best part expandable upto 400gb
6)Network-Dual Volte support and no issue in network.
7)Battery-Low to medium usage one day, Heavy usage - 8 to 9 hrs.
8)Fingerprint and Face unlock- fingerprint is very fast, Face unlock present in smartlock under security.
9)Camera-16+5mp rear is fabulous in daylight(Check my attached pics) Front camera is also pretty good. In lowlight the camera performance is average.Bothie and PIP mode present and its pretty good. Bookeh is okay. 
CONS:
1)Camera-the camera struggles in lowlight but for a better performance use Google Camera(Gcam) app for Nokia 6.1plus (It works in all nokia phones) and the camera performance both daylight and lowlight becomes brilliant. Must use and the camera improves drastically. 
2) The phone heats up after camera use of more than 10mins.
3)Bcz of glass black, the phone is very slippery but it is not a fingerprint magnet like other smartphones. A hard case is a must. 
4)Volume is  low. Could have been more but sound is crisp and audible. 
5)Charger is not fast. But neither slow too.. Type C cable is a welcome and charges better than micro usb. Takes 30mins for 40% charge! 
6)Earphone is average. Same earphone dat used to come 5-6 years ago. 
My Rating - 4.75/5
Check my images for camera pics by Nokia 6.1Plus to get an idea. 
Nokia at its best!! Must buy product from Nokia. 
Awesome!
All specifications are very good in this price. External Design is excellent. After all everyone knows about Nokia Hardware reliability.
Finally, I always like the Nokia brand from my Engineering college life.
I am happy after getting this Nokia 6.1 plus. 
Loved it.. Nokia 😍🤩 
Pros:-
 awesome AI camera
crispier display
good battery backup
multitasking is top-notch no lags
hybrid sim slot
the Nokia branding in back😍
the speaker is loud
awesome for gaming
no heat issue
quick charge
android one platform
cons:-
poor earphones
no silicon case and screen protector 
awesome Nokia 6.1plus handset 
This is one of good smart phones in this range.. 
Feels very premium. 
Camera, Display, Design are awesome. 
Battery drains quickly. 
normal usage will lasts maximum one full day
Proximity sensor stopped working after 3 months of use. 
Charging will not work sometimes. 
Never expected this quality from Nokia.
Trying to change mobile now. 
Best phone ever 
Fabulous Job done Nokia 😘😘 
Stunning and value for money.. 
Page 2
Good Job Nokia
Everything good camera little bit poor in low light
Battery backup not bad. Its work okk
Lookwise awesome
Speed also good 
I have been using this Nokia 6.1 Plus for almost a week now and here is my unbiased review for this product. Couple of Camera stills are attached (one in artificial day light condition and other at night in low light using flash)
...

最新更新