7

我想试试我的手和网页抓取。我注意到 Anglesharp 非常适合 .Net 环境。我正在尝试从 yelp 网站获取所有描述和评级的列表,但没有收到任何错误或任何结果。这是 html 的一部分(在“ https://www.yelp.ca/biz/walmart-toronto-12 ”中有更详细的说明):

<div class="rating-very-large">
    <i class="star-img stars_2" title="2.0 star rating">
        <img alt="2.0 star rating" class="offscreen" height="303" src="//s3-media4.fl.yelpcdn.com/assets/srv0/yelp_styleguide/c2252a4cd43e/assets/img/stars/stars_map.png" width="84">
    </i>
        <meta itemprop="ratingValue" content="2.0">
</div>
<p itemprop="description" lang="en">This Walmart still terrifies me<br><br>Baby things can be found on the back right of the lower level. Godspeed.</p> 

<div class="rating-very-large">
    <i class="star-img stars_1" title="1.0 star rating">
        <img alt="1.0 star rating" class="offscreen" height="303" src="//s3-media4.fl.yelpcdn.com/assets/srv0/yelp_styleguide/c2252a4cd43e/assets/img/stars/stars_map.png" width="84">
    </i>
        <meta itemprop="ratingValue" content="1.0">
</div>
<p itemprop="description" lang="en">Wow I don&#39;t even know where to begin, </p> 

这是我的查询:

var config = var config = new Configuration().WithJavaScript().WithCss();
var parser = new HtmlParser(config);
var document = await BrowsingContext.New(config).OpenAsync("https://www.yelp.ca/biz/walmart-toronto-12");

//Do something with LINQ
var descriptionListItemsLinq = document.All.Where(m => m.LocalName == "p" && m.Id.Contains("description"));
foreach (var element in descriptionListItemsLinq)
{
    element.Text().Dump();
}

如何获取用户评论(描述)和评分列表?

4

1 回答 1

0

我检查了https://www.yelp.ca/biz/walmart-toronto-12HTML的来源。正如我所料,用户评论是有格式的。您不应该在这种情况下使用。JSONAngleSharp

下面的照片是从HTML来源中提取的。

在此处输入图像描述

这是解析后的版本JSON

在此处输入图像描述

它是 a JSON,您可以使用Newtonsoft.Json. 只需提取JSON并从中读取您需要的内容。

于 2016-11-28T20:45:14.087 回答