Skip to content

Instantly share code, notes, and snippets.

View macloo's full-sized avatar
💭
Teaching some Python

Mindy McAdams macloo

💭
Teaching some Python
View GitHub Profile
@macloo
macloo / scraper_boilerplate.py
Created February 22, 2024 18:54
Imports and starter code for a scraping script with BeautifulSoup and Requests
from bs4 import BeautifulSoup
import requests
hdr = {'User-Agent': 'your user-agent info here'}
# find YOUR user-agent HERE: https://www.whatismybrowser.com/detect/what-is-my-user-agent/
url = 'https://www.some_domain.com/some_dir'
page = requests.get(url, headers=hdr)
soup = BeautifulSoup(page.text, 'html.parser')
'''
@macloo
macloo / database.php
Created January 17, 2024 16:04
Database connection script for PHP 8.x and MySQL
<?php
// allow error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<?php
try {
// connect to database
@macloo
macloo / partial_db_commands.py
Last active April 21, 2023 21:47
Some examples of database queries with SQLAlchemy 2.0
# lots of code missing here! Just showing db commands
# my model - table named socks - the model matches the real table in your db
class Sock(db.Model):
__tablename__ = 'socks'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
style = db.Column(db.String)
color = db.Column(db.String)
quantity = db.Column(db.Integer)
@macloo
macloo / wikipedia_page_api.py
Created April 18, 2023 15:27
Access the Wikipedia API with Python, Flask, and Requests
"""
Flask example using Requests library syntax to build
a Wikipedia API query - no templates, just raw HTML
written straight into the browser.
This code was partly written by ChatGPT.
"""
from flask import Flask, jsonify, request, redirect, url_for
import random, requests
@macloo
macloo / wikipedia_API.py
Last active March 1, 2022 23:44
Demonstrates extraction of JSON data from a Wikipedia API request - does NOT use the Python library wikipedia-api
"""Demonstrates extraction of data from a Wikipedia API request"""
import requests
API_URL = 'https://en.wikipedia.org/w/api.php?action=query&origin=*&format=json&generator=search&gsrnamespace=0&gsrlimit=10&gsrsearch={}'
search_term = "David_Bowie"
data = requests.get(API_URL.format(search_term)).json()
@macloo
macloo / download_images.py
Last active November 16, 2023 15:58
Use Wikipedia API to download the main image for a page
"""
Find the main image on a Wikipedia page and download it.
Using a list of Wikipedia URLs, download the main image from each page in the list.
Name the downloaded file to match the page URL.
"""
import requests, os
# set the folder name where images will be stored
my_folder = 'wiki_images'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> Detect 404 </title>
</head>
<body>
<p>Hello.</p>
@macloo
macloo / president.html
Created March 18, 2020 13:36
Starter template for Flask + Jinja
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ pres["President"] }}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}">
</head>
<body>
@macloo
macloo / pres_video2.py
Created March 17, 2020 15:19
Basic Flask app before adding templates
from flask import Flask
app = Flask(__name__)
import csv
def convert_to_dict(filename):
datafile = open(filename, newline='')
my_reader = csv.DictReader(datafile)
list_of_dicts = list(my_reader)
datafile.close()
@macloo
macloo / fetch_example.js
Last active October 31, 2019 22:13
Short and sweet example of using fetch and an API
// from https://frontendmasters.github.io/bootcamp/ajax - more info there
const BREEDS_URL = "https://dog.ceo/api/breeds/image/random";
const promise = fetch(BREEDS_URL);
promise
.then(function(response) {
const processingPromise = response.json();
return processingPromise;