Я пытаюсь получить значение атрибута data-content_published_date, который является датой публикации статьи с xPath, но по какой-то причине я не могу!

Вот как я пытался:

//div[@id='page-segment-values']/@data-content_published_date

А вот тег div:

<div id="page-segment-values" class="">
  <div class="keyvals" data-content_author_name="Eli Meixler" data-content_cms_id="5461956" data-content_headline="British Academic" data-content_modified_date="" data-content_published_date="2018-11-22T08:15:43.000Z" data-content_shown_on_platform="own" data-content_type="article" data-path="/5461956/british-academic-sentenced-spying-united-arab-emirates/" data-referrer="" data-search="" data-content_is_post="post" data-title="A British Academic Has Been Sentenced to Life in Prison on Espionage Charges in the United Arab Emirates" data-affiliate_link_count="0" data-content_cms_category="World" data-content_cms_tags="onetime|overnight|United Arab Emirates" data-content_cms_terms="World,onetime,overnight,United Arab Emirates" data-time_inc_brand="time.com" data-time_inc_application="front end" data-content_syndicated="false" data-content_syndicated_brand="" data-content_syndicated_url="" data-content_nlp_sentiment_label="negative" data-content_nlp_sentiment_score="-0.2" data-content_nlp_sentiment_magnitude="5.9" data-content_nlp_entities="Matthew Hedges" data-content_nlp_payload="{&quot;entities&quot;:[{&quot;type&quot;:&quot;PERSON&quot;,&quot;text&quot;:&quot;Matthew Hedges&quot;,&quot;relevance&quot;:0.57477295,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;WORK_OF_ART&quot;,&quot;text&quot;:&quot;British Academic Sentenced to Life on Spying Charges&quot;,&quot;relevance&quot;:0.02937225,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;LOCATION&quot;,&quot;text&quot;:&quot;British&quot;,&quot;relevance&quot;:0.029201617,&quot;disambiguation&quot;:{&quot;mid&quot;:&quot;\/m\/07ssc&quot;,&quot;wikipedia_url&quot;:&quot;https:\/\/en.wikipedia.org\/wiki\/United_Kingdom&quot;}},{&quot;type&quot;:&quot;PERSON&quot;,&quot;text&quot;:&quot;academic&quot;,&quot;relevance&quot;:0.020707963,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;OTHER&quot;,&quot;text&quot;:&quot;life&quot;,&quot;relevance&quot;:0.018888554,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;OTHER&quot;,&quot;text&quot;:&quot;spying charges&quot;,&quot;relevance&quot;:0.013851213,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;LOCATION&quot;,&quot;text&quot;:&quot;prison&quot;,&quot;relevance&quot;:0.0133453375,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;LOCATION&quot;,&quot;text&quot;:&quot;United Arab Emirates&quot;,&quot;relevance&quot;:0.011457251,&quot;disambiguation&quot;:{&quot;wikipedia_url&quot;:&quot;https:\/\/en.wikipedia.org\/wiki\/United_Arab_Emirates&quot;,&quot;mid&quot;:&quot;\/m\/0j1z8&quot;}},{&quot;type&quot;:&quot;OTHER&quot;,&quot;text&quot;:&quot;threats&quot;,&quot;relevance&quot;:0.009783207,&quot;disambiguation&quot;:{}},{&quot;type&quot;:&quot;EVENT&quot;,&quot;text&quot;:&quot;blowback&quot;,&quot;relevance&quot;:0.009783207,&quot;disambiguation&quot;:{}}],&quot;categories&quot;:[{&quot;label&quot;:&quot;\/Law &amp; Government\/Public Safety\/Crime &amp; Justice&quot;,&quot;score&quot;:0.64}],&quot;docSentiment&quot;:{&quot;magnitude&quot;:5.9,&quot;score&quot;:-0.2,&quot;label&quot;:&quot;negative&quot;},&quot;language&quot;:&quot;en&quot;}" data-content_nlp_categories="/Law &amp; Government/Public Safety/Crime &amp; Justice"></div>
</div>

У кого-нибудь есть идеи, почему я не могу оценить их ценность? И как это получить?

0
Ali 30 Май 2019 в 11:53

2 ответа

Лучший ответ

Используйте внутренний элемент <div> и атрибут, который вы ищете.

//div[@class='keyvals']/@data-content_published_date
1
KunduK 30 Май 2019 в 11:03

Вы пропустили вложенный элемент div, поэтому вы пытаетесь получить значение data-content_published_date внешнего div.

Правильный запрос XPath должен быть

//div[@id='page-segment-values']/div/@data-content_published_date

Или даже точнее

//div[@id='page-segment-values']/div[@class='keyvals']/@data-content_published_date
1
Andy Korneyev 30 Май 2019 в 08:59