我怎样才能从网站“flipkart.com”中删除评论

问题描述 投票:-1回答:1

我无法理解选择哪个类,我尝试了不同的选择器类,但它返回空列表

我试过以下代码。

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")

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

python-3.x web-scraping
1个回答
0
投票

你永远不会在你说过的代码中定义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)

...
© www.soinside.com 2019 - 2024. All rights reserved.