I want to accomplish with goquery the same what is done by the following Python code (the xpath in the comment specifies what is my target):
from requests import get
from bs4 import BeautifulSoup
#/html/body/div[3]/div[3]/div[5]/div[1]/table[2]/tbody/tr[3]/td[2]
def getSnatch49women():
r = get("https://en.wikipedia.org/wiki/List_of_Olympic_records_in_weightlifting")
soup = BeautifulSoup(r.text, 'html.parser')
div = soup.find("div", attrs={"id":"mw-content-text"}).find_all("div")[0]
record = div.find_all("tbody")[1].find_all("tr")[2].find_all("td")[1].get_text()
return record
print(getSnatch49women())#Prints "94 kg"
I've been trying to utilize goquery's Eq()
instead of BeautifulSoup's
find_all()[]
but I'm not getting the result I need. My Golang code:
package main
import ( "fmt"
"log"
"net/http"
"github.com/PuerkitoBio/goquery"
)
func getSnatch49() string {
var record string
res, e :=
http.Get("https://en.wikipedia.org/wiki/List_of_Olympic_records_in_weightlifting")
if e != nil { fmt.Println(e.Error()) }
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("failed to fetch data: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
doc.Find("#mw-content-text div").Eq(0).Eq(8).Find("tbody").Eq(2).Eq(1).Each(func(i int, s *goquery.Selection) {
record = s.Text()
})
return record
}
func main() {
fmt.Println(getSnatch49())
}