admin管理员组文章数量:1320661
const express = require("express");
const request = require("request-promise");
const app = express();
const PORT = process.env.PORT || 5000;
const generateApiKey= (apiKey) => `=${apiKey}&autoparse=true`
app.use(express.json());
app.get("/f1", async (req, res) => {
const {apiKey}=req.query;
try {
const response = await request(`${generateApiKey}&url=`);
res.json(JSON.parse(response));
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).send(`${error}`);
}
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
I am trying to create an API that scrapes data from the Formula 1 website (e.g., team pages) and returns it in JSON format using Node.js and ScraperAPI. My current approach is to use request-promise with ScraperAPI to fetch the page, but I'm encountering issues where the data is not being parsed correctly into JSON for some reason This what I get instead of JSON format with the data Picture with what I get in return
const express = require("express");
const request = require("request-promise");
const app = express();
const PORT = process.env.PORT || 5000;
const generateApiKey= (apiKey) => `http://api.scraperapi?api_key=${apiKey}&autoparse=true`
app.use(express.json());
app.get("/f1", async (req, res) => {
const {apiKey}=req.query;
try {
const response = await request(`${generateApiKey}&url=https://www.formula1/en/teams/ferrari`);
res.json(JSON.parse(response));
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).send(`${error}`);
}
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
I am trying to create an API that scrapes data from the Formula 1 website (e.g., team pages) and returns it in JSON format using Node.js and ScraperAPI. My current approach is to use request-promise with ScraperAPI to fetch the page, but I'm encountering issues where the data is not being parsed correctly into JSON for some reason This what I get instead of JSON format with the data Picture with what I get in return
Share Improve this question edited Jan 18 at 18:54 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Jan 18 at 16:35 TinoOoTinoOo 13 bronze badges 1 |1 Answer
Reset to default 0The issue is in the URL.
In your generateApiKey
function, you're returning a URL string, but in your app.get("/f1", ...)
route, you're not dynamically passing the API key into that URL correctly. Additionally, in your request call, you're referencing generateApiKey
without actually calling it with the apiKey
parameter. Furthermore, while the target URL (https://www.formula1/en/teams/ferrari
) appears valid, it’s a good practice to encode URLs to prevent issues with special characters in other cases. It's also important to note that the Formula 1 team page likely returns raw HTML, which cannot be directly parsed into JSON unless the page itself provides a structured JSON API response. If the data is embedded in HTML, you may need to use libraries like cheerio
to parse the HTML and extract the relevant structured data.
try this:
const express = require("express");
const request = require("request-promise");
const app = express();
const PORT = process.env.PORT || 5000;
// Function to generate the ScraperAPI URL with API Key
const generateApiKey = (apiKey) => `http://api.scraperapi?api_key=${apiKey}&autoparse=true`;
app.use(express.json());
app.get("/f1", async (req, res) => {
const { apiKey } = req.query;
if (!apiKey) {
return res.status(400).send("API key is required");
}
const targetUrl = "https://www.formula1/en/teams/ferrari";
try {
const response = await request(`${generateApiKey(apiKey)}&url=${encodeURIComponent(targetUrl)}`);
// If the response is raw HTML, you may need to parse it further
try {
res.json(JSON.parse(response)); // Attempt to parse JSON
} catch (parseError) {
res.send(response); // If not JSON, send raw response (likely HTML)
}
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).send(`Error fetching data: ${error.message}`);
}
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
版权声明:本文标题:javascript - How to Scrape and Convert F1 Website Data into JSON Using Node.js and ScraperAPI? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742064434a2418748.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
await
both the call torequest()
and the call tores.json()
. Theres.json()
call needs no argument and you do not need to callJSON.parse()
at all. – Pointy Commented Jan 18 at 16:39