Wiki page scraping using Sheets (why is query not always working right?)

Viewed 66

so with the help of the ppl here I've got this query for scraping a certain section from wiki page, but I've noticed it's not always working right. In some cases, it's scraping other data points sections in the page, and I can't figure out why. I only wanna get the 'date of death' section (if the page has one), but in some cases it returns smth else.

=IFERROR(QUERY(IMPORTXML("https://www.wikidata.org/wiki/"&A2, "//*"), 
 "select Col2 where Col1 = 'date of death' and Col2 is not null"),FALSE)

Is there a way I can improve the formula/query to increase its accuracy? Also, is there a way to make it work faster (on my main sheet when I apply it to loads of data it's taking very long to update). Here's the sample sheet. Thanks

1 Answers

try:

=IFERROR(REGEXEXTRACT(QUERY(ARRAY_CONSTRAIN(IMPORTXML(
 "https://www.wikidata.org/wiki/"&A7, "//*"), 1000, 2), 
 "select Col2 
  where Col1 matches 'date of death' 
    and Col2 is not null", 0), "\s(.*)\s\d.*reference.*"), FALSE)

enter image description here

Related