LoginSignup
1
1

More than 1 year has passed since last update.

ChatGPTでスクレイピングをする(その1)

Posted at

ChatGPTを使ってスクレイピングをする。またはスクレイピングを行うコードを出力する。

ケース1. 単純なHTMLの表

質問:

Scrape following html and answer my question. How old is Jane Doe?

name age
John Doe 30
Jane Doe 29

回答: According to the html provided, Jane Doe is 29 years old.

評価:正解

ケース2. JavaScriptのプログラムを生成

質問: Could you show me the source code for scraping age in JavaScript?

回答:
Certainly! Here is some code that would allow you to scrape the age of Jane Doe from the HTML you provided using JavaScript:

const table = document.querySelector("table");
const rows = table.querySelectorAll("tr");

let janeDoeRow;
for (const row of rows) {
  const nameCell = row.querySelector("td");
  if (nameCell.textContent === "Jane Doe") {
    janeDoeRow = row;
    break;
  }
}

const ageCell = janeDoeRow.querySelector("td:nth-child(2)");
const age = ageCell.textContent;

console.log(`Jane Doe is ${age} years old.`);

This code first selects the table element and then finds all of its rows. It then loops through the rows and finds the one that contains the name "Jane Doe". Once it finds the correct row, it selects the second cell (which contains the age) and logs the age to the console.

I hope this helps! Let me know if you have any questions.

評価: 正解。でもこれだと出力がハードコードされてしまっている。

ケース3: ハードコードしないプログラムの生成

質問: Could you show me the source code for scraping the list of names and ages from above html in JavaScript?

回答: Certainly! Here is some code that would allow you to scrape the names and ages from the HTML you provided using JavaScript:

const table = document.querySelector("table");
const rows = table.querySelectorAll("tr");

const namesAndAges = {};

for (const row of rows) {
  const nameCell = row.querySelector("td");
  const ageCell = row.querySelector("td:nth-child(2)");

  if (nameCell && ageCell) {
    const name = nameCell.textContent;
    const age = ageCell.textContent;

    namesAndAges[name] = age;
  }
}

console.log(namesAndAges);

評価: 正解

ケース4: スクレイピング結果をSQLに変換

質問: Could you show me the SQL script for scraping the above html, extracting the names and ages and storing the data in a SQL database?

回答: Here is a SQL script that would allow you to scrape the names and ages from the HTML you provided and store the data in a SQL database:

CREATE TABLE people (
  name VARCHAR(255),
  age INT
);

INSERT INTO people (name, age)
VALUES ('John Doe', 30),
       ('Jane Doe', 29);

評価: 正解

まとめ

まぁ、これは出来るだろうなと思ってました(偉そう)。続く

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1