If you are looking into the requests and responses from such a website, mostly you will find something with json.
You have to look for the headers (and in case of POST-request for the data), in this quontigo-example it looks like
POST /wp-admin/admin-ajax.php HTTP/2
Host: qontigo.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0
Accept: */*
Accept-Language: de-DE,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br
Referer: https://qontigo.com/index/stxwagr/
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 60
Origin: https://qontigo.com
DNT: 1
Connection: keep-alive
Cookie: OptanonConsent=isIABGlobal.....blablabla.....&consentId=xxxxxxxxxxxxx.....
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
TE: trailers
action=index_detail_refresh&index_id=51174&isin=CH0462362101
In order to be able to load the data reproducibly, a suitable cookie must always be used in this case.
To do this, you have to use curl twice, once to get a fresh cookie, and the second time to load the desired data with this cookie.
curl --cookie-jar quontigo.txt "https://qontigo.com/index/stxwagr/"
–cookie-jar saves the received cookie in the file quontigo.txt
curl --compressed --cookie quontigo.txt -H "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0" -H "Accept: */*" -H "Accept-Language: de-DE,en-US;q=0.7,en;q=0.3" -H "Accept-Encoding: gzip, deflate, br" -H "Referer: https://qontigo.com/index/stxwagr/" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "X-Requested-With: XMLHttpRequest" -H "Content-Length: 60" -H "Origin: https://qontigo.com" -H "DNT: 1" -H "Connection: keep-alive" -H "Sec-Fetch-Dest: empty" -H "Sec-Fetch-Mode: cors" -H "Sec-Fetch-Site: same-origin" -H "TE: trailers" -d "action=index_detail_refresh" -d "index_id=51174" -d "isin=CH0462362101" "https://qontigo.com/wp-admin/admin-ajax.php"
–cookie uses the data from quontigo.txt
quontigo.go:
package main
import (
"log"
"net/http"
"fmt"
"os/exec"
)
func gethistory(w http.ResponseWriter, req *http.Request) {
getcookie := "curl --cookie-jar quontigo.txt \"https://qontigo.com/index/stxwagr/\""
body, err := exec.Command("bash", "-c", getcookie).Output()
if err != nil {
log.Fatalln(err)
}
getjson := "curl --compressed --cookie quontigo.txt -H \"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0\" -H \"Accept: */*\" -H \"Accept-Language: de-DE,en-US;q=0.7,en;q=0.3\" -H \"Accept-Encoding: gzip, deflate, br\" -H \"Referer: https://qontigo.com/index/stxwagr/\" -H \"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\" -H \"X-Requested-With: XMLHttpRequest\" -H \"Content-Length: 60\" -H \"Origin: https://qontigo.com\" -H \"DNT: 1\" -H \"Connection: keep-alive\" -H \"Sec-Fetch-Dest: empty\" -H \"Sec-Fetch-Mode: cors\" -H \"Sec-Fetch-Site: same-origin\" -H \"TE: trailers\" -d \"action=index_detail_refresh\" -d \"index_id=51174\" -d \"isin=CH0462362101\" \"https://qontigo.com/wp-admin/admin-ajax.php\""
body, err = exec.Command("bash", "-c", getjson).Output()
if err != nil {
log.Fatalln(err)
}
jsonstring := string(body)
fmt.Fprintf(w, jsonstring)
}
func headers(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
}
func main() {
http.HandleFunc("/", gethistory)
http.HandleFunc("/headers", headers)
http.ListenAndServe(":8083", nil)
}
So, you have to use
$.data.chart_data[*][0]
$.data.chart_data[*][1]
You can do this to have the data complete once, but because this method always loads all data (since 1997), it is quite slow and actually stupid.
It would be better (for quontigo and for you) to use the link from @Abacus6923. This is a csv, but you can change that.
quontigo2.go:
package main
import (
"io/ioutil"
"encoding/json"
"strings"
"log"
"net/http"
"fmt"
)
var nlines int = 0
type Record struct {
Date string `json:"date"`
Index string `json:"index"`
Value string `json:"value"`
}
func h_3m_from_stoxx(w http.ResponseWriter, req *http.Request) {
geturl := "https://www.stoxx.com/document/Indices/Current/HistoricalData" + req.URL.Path + ".txt"
resp, err := http.Get(geturl)
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
jsonstring := string(body)
records := []Record{}
lines := strings.Split(jsonstring, "\n")
for key, _ := range lines[0:] {
nlines = key
}
for _, line := range lines[1:nlines] {
fields := strings.Split(line, ";")
date := fields[0]
index := fields[1]
value := fields[2]
records = append(records, Record{
Date: date,
Index: index,
Value: value,
})
}
jsonData, err := json.Marshal(records)
if err != nil {
panic(err)
}
fmt.Fprintf(w, string(jsonData))
}
func headers(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
}
func main() {
http.HandleFunc("/", h_3m_from_stoxx)
http.HandleFunc("/headers", headers)
http.ListenAndServe(":8084", nil)
}
$[*].date
$[*].value
HTH