Open lnxpy opened 1 month ago
To scrape data from Wikipedia using JavaScript, you can use the axios
library to make HTTP requests and cheerio
for parsing HTML content. Here is a simple example:
npm install axios cheerio
scrape.js
) and add the following code:const axios = require('axios');
const cheerio = require('cheerio');
// URL of the Wikipedia page to scrape
const url = 'https://en.wikipedia.org/wiki/Web_scraping';
axios.get(url)
.then(response => {
const $ = cheerio.load(response.data);
// Example: Scrape the title of the Wikipedia page
const pageTitle = $('title').text();
console.log('Page Title:', pageTitle);
// You can further parse the HTML content to extract specific data
// For example, to scrape all the links on the page
$('a').each((index, element) => {
const link = $(element).attr('href');
console.log('Link:', link);
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
node scrape.js
This script makes an HTTP GET request to the specified Wikipedia page, loads the HTML content using Cheerio, and then extracts and prints the title of the page and all the links on that page. You can modify the parsing logic to scrape other specific data as needed.
Text
Prompt
assist with code block examples and explanations
Submission Privacy