I’m trying to scrape data on the transfermarket website, but it’s giving me the error I posted.
I’m using Javascript, Cheerio and Axios.
const fetchData = async(url) => { const result = await axios.get(url) return result.data } const main = async () => { const content = await fetchData("https://www.transfermarkt.com.br/premier-league/torschuetzenliste/wettbewerb/GB1/saison_id/2022") const $ = cheerio.load(content) let artilheiros = [] $('table.items tbody tr').each((i, e) => { const AllElements = $(e).find('td') const nomeArtilheiro = $(`${AllElements[1]} > table > tbody > tr.hauptlink > a`).text(); // const clubeArtilheiro // const jogos // const gols const data = {nomeArtilheiro} artilheiros.push(data) }) console.log(artilheiros) } main()
The error it gave:
throw new Error("Attribute selector didn't terminate"); Error: Attribute selector didn't terminate
Advertisement
Answer
This line is the error:
const nomeArtilheiro = $(`${AllElements[1]} > table > tbody > tr.hauptlink > a`).text();
AllElements[1]
is not a string, so its probably turning into "[object Object]"
You should write
$(AllElements[1]).find('table > tbody > tr.hauptlink > a').text()