Python

楽天モバイル(奈良県)の包括免許の差分をツイート

インストール pip install pandas pip install requests pip install tweepy プログラム # -*- coding: utf-8 -*- import csv import datetime import pathlib import urllib.parse import pandas as pd import requests import tweepy # Twitter consumer_k…

無線局等情報検索(Web-API機能)を利用し基地局情報をデータラングリング

www.tele.soumu.go.jp 件数取得API/一覧取得APIのリクエスト条件一覧 コード値一覧 地方公共団体コード import urllib.parse import requests import pandas as pd d = { # 1:免許情報検索 2: 登録情報検索 "ST": 1, # 詳細情報付加 0:なし 1:あり "DA": 1, …

今治市の保育所等の入所可能状況をマップ化

今治市 保育幼稚園課の保育所等の今治市受け入れ可能状況一覧のPDFの表をスクレイピング後地図に表示 今治市オープンデータ一覧に保育園、認定こども園の住所と位置情報があるが全部ではなかったのでスプレッドシート作成 スプレッドシート 今治市内保育園(…

Python整形オンライン

https://black.now.sh/ https://yapf.now.sh/

Pandasでグループ別にファイル保存

import pandas as pd df = pd.read_csv("2018.tsv", sep='\t') grouped_df = df.groupby("id") # print(grouped_df.groups) for id in grouped_df.groups: d = grouped_df.get_group(id) # ファイル名作成 filename = str(id) + ".tsv" # ファイル保存 d.to_…

i.river.go.jpからダム情報と河川情報をスクレイピング

2021/04/18現在利用できません import re import requests from bs4 import BeautifulSoup headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" } def scraping(url, pattern): r = requests.get(url, head…

JFLゴール数ランキング(GoogleDrive)

imabari.hateblo.jp import csv import time from urllib.parse import urljoin import gspread import pandas as pd import requests from bs4 import BeautifulSoup from oauth2client.service_account import ServiceAccountCredentials from tqdm import…

JFLランキング作成(GoogleDrive)

pip3 install pandas pip3 install beautifulsoup4 pip3 install html5lib pip3 install lxml pip3 install gspread pip3 install oauth2clinet pip3 install tqdm # -*- coding: utf-8 -*- import gspread import pandas as pd import requests from bs4 im…

スクレイピングのおけるCSSセレクタ基本

サンプル <div class="class" id="id"> <h1>タイトル</h1> <h2>サブタイトル</h2> <p value="abc">テスト</p> <p value="abc def">テスト</p> <p value="abc-def">テスト</p> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </div> 基本 書式 説明 サンプル * すべての要素 * 要素名 要素名の要素 div .クラス名 id属性をつけた要素 div.class #id名 id属性をつけた要素 div#id セレクタ同士の関係 書式 説明 サンプ…

Pythonでスクレイピングまとめ

いまからスクレイピングをはじめるならrequests-htmlがおすすめ Requests-HTML: HTML Parsing for Humans (writing Python 3)! — requests-HTML v0.3.4 documentation imabari.hateblo.jp imabari.hateblo.jp imabari.hateblo.jp imabari.hateblo.jp imabari…

Beautifulsoupでrowspan・colspanにデータ挿入

import csv import requests from bs4 import BeautifulSoup url = 'https://www.pref.ehime.jp/h25115/kanjyo/topics/influ1819/tb_flu1819.html' # セルコピー True:空白、False:コピー flag = False headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT …

本文抽出

kanji.hatenablog.jp github.com import time import requests from bs4 import BeautifulSoup from extractcontent3 import ExtractContent headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko' } def scr…

requests-htmlでスクレイピング

requests-html https://html.python-requests.org/ !pip install requests-html !pip install retry 日経平均をスクレイピング from requests.exceptions import ConnectionError, TooManyRedirects, HTTPError from requests_html import HTMLSession from …

json feed

import requests import json url = 'http://www.city.seiyo.ehime.jp/kinkyu/index.update.json' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko' } r = requests.get(url, headers=headers) data = r…

Beautifulsoupでスクレイピング

Beautifulsoupの方がひとつのファイルですむのでやっぱり楽 import datetime import os import re import string import time from urllib.parse import urljoin import requests from bs4 import BeautifulSoup headers = { 'User-Agent': 'Mozilla/5.0 (Wi…

Scrapyでスクレイピング

普段Beautifulsoupしか使ってないのでScrapyで作成してみた github.com をベースに作成しました。 note.nkmk.me https://doc.scrapy.org/en/latest/topics/commands.html # インストール pip install scrapy # プロジェクト作成 scrapy startproject cheerup…

玉川ダム・蒼社川(水位)・テレメーター(雨量)

2021/04/18現在利用できません i.river.go.jpから正規表現で抽出 Twitterの文字数多くなったのでぎりぎりいけそう import re import time import requests import twitter from bs4 import BeautifulSoup headers = { 'User-Agent': 'Mozilla/5.0 (Windows N…

ボランティア数集計

!pip install lxml !pip install seaborn !apt install fonts-ipafont-gothic !rm /content/.cache/matplotlib/fontList.json import pandas as pd url = 'https://ehimesvc.jp/?p=70' dfs = pd.read_html(url, index_col=0, na_values=['活動中止', '終了',…

鹿野川ダムと野村ダムと肱川をスクレイピング

2021/04/18現在利用できません インストール pip install requests pip install python-twitter pip install apscheduuler pip install beautifulsoup4 プログラムを実行すると8,18,28,38,48,58分に表示 コメントアウトしているTwitterのキーを入力すると投…

玉川ダムと蒼社川の水位

2021/04/18現在利用できません import datetime import requests import twitter from bs4 import BeautifulSoup # 文字を小数点に変換、変換できない場合は0.0 def moji_float(x): try: result = float(x) except: result = 0.0 return result # 空文字の場…

愛媛県河川・水防情報よりダム情報をスクレイピング

進捗状況を表示するように変更 import csv import datetime import time import requests from bs4 import BeautifulSoup from tqdm import tqdm def date_span(start_date, end_date, hour_interval): res = [] n = start_date while n < end_date: n += da…

asyncioでスクレイピングを高速化

import asyncio import aiohttp import requests from bs4 import BeautifulSoup async def scraping(url): async with aiohttp.ClientSession() as session: async with session.get(url) as response: html = await response.text() soup = BeautifulSoup(…

ThreadPoolExecutorでスクレイピングを高速化

from concurrent.futures import ThreadPoolExecutor import requests from bs4 import BeautifulSoup def scraping(url): r = requests.get(url) if r.status_code == requests.codes.ok: soup = BeautifulSoup(r.content, 'html5lib') result = [] return …

PythonのrequestsでPOST送信スクレイピング

今治地区の救急病院をスクレイピングし曜日別・医療機関別に集計する seleniumを使わずにrequestsでpost送信 Firefoxの開発ツールでpost内容を確認 ネットワークの中からメソッドPOSTを選びパラメーターのフォームデータを確認 "blockCd[3]": "", "forward_n…

JFLの試合結果分析

github.com

今治の校区別人口を地図に表示

今治市オープンデータ一覧 | 情報政策課 オープンデータ | 今治市 平成30年度の今治市の住民基本台帳人口統計 http://www.city.imabari.ehime.jp/opendata/toukei_jinkou/201805.xls 小・中学校 http://www.city.imabari.ehime.jp/opendata/data/school.csv …

FC今治のゴール集計・先取点

!pip install lxml !apt install fonts-ipafont-gothic !rm /content/.cache/matplotlib/fontList.json """再起動""" import time import csv import requests from bs4 import BeautifulSoup # 試合数 n = 10 + 1 with open('fcimabari_goal.tsv', 'w') as …

PythonでスクレイピングしてテーブルをCSVに保存

Beautifulsoupの場合 import csv from bs4 import BeautifulSoup import requests url = 'http://www.example.com/' r = requests.get(url) if r.status_code == requests.codes.ok: soup = BeautifulSoup(r.content, 'html.parser') result = [[[td.get_tex…

Pandasでミニロトで遊ぶ

!pip install lxml import pandas as pd import io import requests # User-agentを設定しないとダウンロードできない url = 'http://www.japannetbank.co.jp/lottery/co/minilotojnb.csv' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64;…

ミニロトの結果を取得

TSV保存 from selenium import webdriver from selenium.webdriver.firefox.options import Options from bs4 import BeautifulSoup import csv options = Options() options.set_headless() driver = webdriver.Firefox(options=options) driver.get( 'http…