メインコンテンツまでスキップ

Scraping Template

· 約1分

GAS

取得

API

const res = UrlFetchApp.fetch('https://example.com');
status = res.getResponseCode();
const json = JSON.parse(res.getContentText());

保存

Spreadsheet

複数行を追加

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('シート名');

sheet
.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length)
.setValues(values);

Python

取得

HTML:requests

def download_html(url, file_path):
res = requests.get(url)
html = res.text
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w') as f:
f.write(html)
return html

def get_html(url, force_download=False):
file_path = f"html/{convert_url_to_filename(url)}.html"

if os.path.isfile(file_path) and not force_download:
with open(file_path, 'r') as f:
html = f.read()
else:
html = download_html(url, file_path)

return html

def convert_url_to_filename(url):
return re.sub(r'/', '_', url)

requests

import requests

res = requests.get('https://example.com')
status = res.status_code
json = res.json()

保存

csv

import csv
with open(f'hoge.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow([
'id', # id
'name', # プロジェクト名
])
writer.writerows(dataList)