lnxpy / mdb-ai-prompted-text

MindsDB (mdb.ai) AI Single-prompting Text Using PyAction
1 stars 0 forks source link

AI: Text Prompting #27

Open lnxpy opened 1 month ago

lnxpy commented 1 month ago

Text

scraping Wikipedia using javascript

Prompt

assist with code block examples and explanations

Submission Privacy

github-actions[bot] commented 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:

  1. Install the required libraries using npm:
npm install axios cheerio
  1. Create a JavaScript file (e.g., 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);
  });
  1. Run the script in the terminal:
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.