0

<a class="bui-link" href="
/hotel/us/springhill-suites-by-marriott-new-york-manhattan-times-square-36th-st.html?label=gen173nr-1FCAEoggI46AdIM1gEaOkBiAEBmAEpuAEXyAEM2AEB6AEB-AELiAIBqAIDuAL96tfrBcACAQ&amp;amp;amp;sid=4c5d8dab6c3bbef3d5d12adeeb051968&amp;amp;amp;all_sr_blocks=412513203_125848231_0_1_0&amp;amp;amp;checkin=2020-05-01&amp;amp;amp;checkout=2020-05-30&amp;amp;amp;dest_id=20088325&amp;amp;amp;dest_type=city&amp;amp;amp;group_adults=2&amp;amp;amp;group_children=0&amp;amp;amp;hapos=1&amp;amp;amp;highlighted_blocks=412513203_125848231_0_1_0&amp;amp;amp;hpos=1&amp;amp;amp;no_rooms=1&amp;amp;amp;sr_order=popularity&amp;amp;amp;srepoch=1568011662&amp;amp;amp;srpvid=02422fc731150043&amp;amp;amp;ucfs=1&amp;amp;amp;from=searchresults;map=1&amp;amp;amp;msd=1
#hotelTmpl" data-google-track="Click/Action: sr_map_link_used" data-coords="-73.993972,40.754011" target="_blank" data-bui-component="Tooltip" data-bui-tooltip-trigger="hover focus" data-tooltip-trigger="hover focus" data-tooltip-position="top" data-tooltip-follow="" data-tooltip-light="" aria-hidden="true" data-map-caption="" rel="noopener" data-tooltip-text="
Hell's Kitchen
 is a great choice for travelers interested in tourist attractions, skyline views and city walks">
 Hell's Kitchen, New York
<span class="sr_card_address_line__item">
<span class="sr_card_address_line__dot-separator"></span>
Show on map</span>
</a>

Вот нужно достать текст, в частности только город New York. Есть 100 отелей нужно из каждого достать город и проверить соответствует шаблону "New York" (проверка Assert) Делал с помощью AngleSharp, но не получается

public static async void GetHtmlAsync()
        {
            var collection = new List<string>();
            var url = "https://www.booking.com/searchresults.en-us.html?label=gen173nr-1FCAEoggI46AdIM1gEaOkBiAEBmAEpuAEXyAEM2AEB6AEB-AELiAIBqAIDuAKAiunrBcACAQ&lang=en-us&sid=5011154a9d347be58cbb69e5b81eaf7a&sb=1&src=index&src_elem=sb&error_url=https%3A%2F%2Fwww.booking.com%2Findex.html%3Flabel%3Dgen173nr-1FCAEoggI46AdIM1gEaOkBiAEBmAEpuAEXyAEM2AEB6AEB-AELiAIBqAIDuAKAiunrBcACAQ%3Bsid%3D5011154a9d347be58cbb69e5b81eaf7a%3Bsb_price_type%3Dtotal%26%3B&ss=New+York&is_ski_area=0&checkin_month=05&checkin_monthday=01&checkin_year=2020&checkout_month=05&checkout_monthday=30&checkout_year=2020&group_adults=2&group_children=0&no_rooms=1&b_h4u_keep_filters=&from_sf=1";

            var httpClient = new HttpClient();
            var html = await httpClient.GetStringAsync(url);

            var htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(html);

            var CityList = htmlDocument.DocumentNode.Descendants("a").
                Where(x => x.Attributes["class"].Value.Contains(("bui-link")));
            foreach (var item in CityList)
            {
                if (item != null)
                {
                    collection.Add(item.LastChild.InnerText.Trim().Replace("-", ""));
                }
                Console.WriteLine("empty");
            }
        }

3
  • но не получается что не получается?
    – tym32167
    13 сен 2019 в 3:40
  • @tym32167 не работает вот эта строка, не знаю как по другому записать var CityList = htmlDocument.DocumentNode.Descendants("a"). Where(x => x.Attributes["class"].Value.Contains(("bui-link"))); думал еще так, но все равно exception var CityList = htmlDocument.DocumentNode.SelectNodes("boolean(//div[@class = 'sr_card_address_line']/a[@class = 'bui-link' and not(contains(text(), 'New York'))])"). Where(x => x.Attributes["class"].Value.Contains(("bui-link")));
    – Yarik
    13 сен 2019 в 4:29
  • Что значит не работает? Ошибка вылетает или что?
    – tym32167
    13 сен 2019 в 7:02

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.