xpath to obtain texts between 2 tags in IMPORTXML formula

狂风中的少年 提交于 2021-02-11 17:01:36

问题


EDIT- I have changed xpaths. odds xpath and result xpath are exactly what I want. But the Race url and race num do not correspond to this data.

I have found an xpath that returns the info that I need but it returns all data and I only need data between Australia and new zealand.

I have 3 xpaths that I need refined.

Sample URL: https://www.punters.com.au/form-guide/2020-02-06/

Race URL XPATH - //*[@class='component-wrapper form-guide-index']/table1/tbody/tr//td/a/@href

Race Result XPATH - //div[@class='upcoming-race__event-info upcoming-race__event-info--has-results']/div[2]/div[2]/div[2]/div/span

Race No XPATH - //*[@class='component-wrapper form-guide-index']/table1/tbody/tr//td/div/div/span

Odds XPATH - //*[@class='component-wrapper form-guide-index']/table/tbody/tr//td/div/div[2]/div[2]/div[4]

The issue is I get all 4 xpaths returning varying lengths of data. I would want all of them to return same lengths of data. If no data, then return blank.

Can someone assist please.Image below to show the data required.


回答1:


For Australia (adapt it to NZ), 4 XPaths of the same length :

URLs (41 elements) :

//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]/a/@href

Results (41 elements) :

//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]/a/text()

Races (30+11 = 41 elements) :

//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]//span[@class="upcoming-race__race-num"]/text()|//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]/a[.="ABD"]/text()

Odds (30+11 = 41 elements) :

//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]//div[@class="result-value-1"]/following-sibling::div[3]/text()|//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]/a[.="ABD"]/text()

For Races and Odds, just replace "ABD" with blank once you get the data in your sheet. Create a new column "City" with the URL column you'll get (keep the text after the second "/" and just before "_").

EDIT : if you don't want "ABD" data, use these (URLS, Results, Races, Odds). 30 elements for each.

//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]/a[not(.="ABD")]/@href

//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]/a[not(.="ABD")]/text()

//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]//span[@class="upcoming-race__race-num"]/text()

//tr[@class="upcoming-race__row"][preceding::tr[@class='upcoming-race__row upcoming-race__row--country'][1][*/.="Australia"]]/td[position()>=2]//div[@class="result-value-1"]/following-sibling::div[3]/text()


来源:https://stackoverflow.com/questions/60577160/xpath-to-obtain-texts-between-2-tags-in-importxml-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!