Skip to content

Instantly share code, notes, and snippets.

@miettal
Created July 16, 2012 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miettal/3123322 to your computer and use it in GitHub Desktop.
Save miettal/3123322 to your computer and use it in GitHub Desktop.
bbsmenuから板一覧を取得する
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import re
import urllib
import sys
import pprint
DEFAULT_BBSMENU_URL = 'http://menu.2ch.net/bbsmenu.html'
def get_2ch_bbs_menu(bbsmenu_url=DEFAULT_BBSMENU_URL) :
res = urllib.urlopen(bbsmenu_url)
p1 = re.compile(r'<BR><BR><B>(.*)</B><BR>')
p2 = re.compile(r'^<A HREF=([0-9a-zA-Z_:/\.]*\.2ch\.net/[0-9a-zA-Z_]*/)>(.*)</A><br>$')
bbs_menu = {}
for x in res.read().decode('sjis').splitlines() :
m1 = p1.match(x)
m2 = p2.match(x)
if m1 :
category = m1.group(1)
bbs_menu[category] = {}
elif m2 :
board_name = m2.group(2)
board_url = m2.group(1)
try :
bbs_menu[category][board_name] = board_url
except NameError :
pass
for x in bbs_menu.keys() :
if bbs_menu[x] == [] :
del bbs_menu[x]
return bbs_menu
if __name__ == "__main__" :
pprint.pprint(get_2ch_bbs_menu())
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment