【问题标题】:Get text inside a span class of a particular div在特定 div 的 span 类中获取文本
【发布时间】:2019-03-25 21:38:46
【问题描述】:

我正在 T-Mobile 网站上搜索有关三星 Galaxy S9 的评论。我可以为 HTML 代码创建一个 Beautiful Soup 对象,但我无法获取跨度类中存在的评论文本,还需要遍历评论页面以收集所有评论。

我尝试了 2 个代码,但一个返回错误,另一个返回一个空列表。我也无法在汤对象中找到我需要的特定跨度类。

from urllib.request import Request, urlopen
from bs4 import BeautifulSoup

tmo_ratings_s9 = []

req = Request('https://www.t-mobile.com/cell-phone/samsung-galaxy-s9', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
tmo_soup_s9 = BeautifulSoup(webpage, 'html.parser')
tmo_soup_s9.prettify()
for review in tmo_soup_s9.find_all(class_="BVRRReviewText"):
    text = review.span.get_text(strip=True)
    tmo_soup_s9.append(text)

print(tmo_ratings_s9)


############################################################################

from urllib.request import urlopen
html = urlopen("https://www.t-mobile.com/cell-phone/samsung-galaxy-s9")

soup=BeautifulSoup(html)

ratings = soup.find_all('div', class_='BVRRReviewTextParagraph BVRRReviewTextFirstParagraph BVRRReviewTextLastParagraph')     
textofrep = ratings.get_text().strip()
tmo_ratings_s9.append(textofrep)

我希望从网页上的所有 8 个页面中获取评论文本并将它们存储在 HTML 文件中。

【问题讨论】:

  • 请提供minimal reproducible example。这将增加您获得适当答案的机会。此外,它还可以帮助您自己解决问题。
  • @shash678:我不知道硒......有没有办法可以在python中刮掉它......特定的div类 Terrible ** 是甚至不存在于汤对象中
  • @shash678:但是当前的代码也不能用于抓取第 1 页的网页评论,应该怎么做?我也在考虑将网页作为 HTML 格式保存在我的本地驱动器上,然后将其废弃……那有可能吗?
  • @SukritSen 看看我的回答

标签: python html web-scraping


【解决方案1】:

首先,如果您使用的是 google chrome 或 mozilla firefox,请在页面中按 ctrl+u,然后您将转到页面源。通过搜索一些关键字来检查评论内容是否存在于源中的任何位置。如果存在,则写入该数据的 xpath,如果不存在,请检查网络部分是否在页面加载时发送任何 json 请求,如果不存在,则必须使用 selenium。

在您的情况下,向此页面发送请求 https://t-mobile.ugc.bazaarvoice.com/9060redes2-en_us/E4F08F7E-8C29-4420-BE87-9226A6C0509D/reviews.djs?format=embeddedhtml

这是加载整个页面时发送的 json 请求。

【讨论】:

  • @Agnus Mathew 谢谢你的见解......你能告诉我你是如何从网站上生成这个链接的吗?我想知道如何创建json请求
  • 如果您使用的是 google chrome 或 mozilla 右键单击​​页面并单击检查,然后在检查选项卡中选择网络并检查保留日志并重新加载页面,然后您将看到期间发送的所有请求页面的加载,从那里您可以搜索您的关键字
【解决方案2】:

由于通过脚本加载动态内容,您无法获取数据。你可以试试 selenium 和 scrapy。

import scrapy
from selenium import webdriver
from scrapy.http import HtmlResponse

class ProductSpider(scrapy.Spider):
    name = "product_spider"
    allowed_domains = ['t-mobile.com']
    start_urls = ['https://www.t-mobile.com/cell-phone/samsung-galaxy-s9']

    def __init__(self):
        self.driver = webdriver.Firefox()

    def parse(self, response):
        self.driver.get(response.url)
        body = str.encode(self.driver.page_source)
        self.parse_response(HtmlResponse(self.driver.current_url, body=body, encoding='utf-8'))

    def parse_response(self, response):
        tmo_ratings_s9 = []
        for review in response.css('#reviews div.BVRRContentReview'):
            text = review.css('.BVRRReviewText::text').get().strip()
            tmo_ratings_s9.append(text)

        print(tmo_ratings_s9)

    def spider_closed(self, spider, reason):
        self.driver.close()

【讨论】:

    【解决方案3】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多