ラベル python の投稿を表示しています。 すべての投稿を表示
ラベル python の投稿を表示しています。 すべての投稿を表示

2013年2月17日日曜日

BeautifulSoup4で遊んでみた

mac portでsearchしてるとbs4っていうのを見かけたので、なんだろ?って 思ったら、互換性も若干無くなってversion4になってたのね。 
使い方を調べて、ついでにとあるCD買ったページで結局何枚買ったかソートするコードを書いてみた。
ちょーいいかげんなコードだけど、こんなのでも簡単にかけた。
import re

if __name__ == "__main__":
    from bs4 import BeautifulSoup
    f = open('5th-cd.html', 'r')

    soup = BeautifulSoup(f)
    soup.unicode
    part_tr = soup.find('table', attrs={'summary': u'ご注文商品'})

    part_tr2 = part_tr.find_parent().find_parent().find_parent()
    events = []
    for num, cd in zip(part_tr2.find_all('th', recursive=True), part_tr2.find_all('td', recursive=True)):
        if re.search(u'点', unicode(num.text)):
            try:
                artist, title1, title2, title3, event_day, event_hall, event_sch, member, extra = cd.text.strip().split()
                event_day = event_day.strip(u'※')
            except ValueError:
                continue   #数があわないのは商品でない
            cd_amount = num.text.strip().strip(u'点')
            try:
                events.append(u"%s, %s, %s, %s, %s" % (event_day, event_sch, member, event_hall, cd_amount))
            except IndexError:
                continue        # 商品以外もテーブルで抜くので一致しないことがある
            except NameError:
                continue
            del cd_amount

    events.sort()
    for event in events:
        print event

2012年3月21日水曜日

PyCharmのテンプレートで使える変数

しょっちゅう忘れてこまるのでメモをする。
PyCharmのテンプレートではIDEにはよくある変数の展開ができるのだけども、
その変数をよく忘れちゃうのよね。調べ方も忘れちゃうし。


  • ${USER} - login name of the current user.
  • ${NAME} - the name of the file that will be created.
  • ${DATE} - the current system date.
  • ${TIME} - the current system time.
  • ${YEAR} - the current year.
  • ${MONTH} - the current month.
  • ${DAY} - the current day of the month.
  • ${HOUR} - the current hour.
  • ${MINUTE} - the current minute.

PyCharm 2.0.0 Web Help



そんな感じで、俺のテンプレート。
#! env python
# -*- coding: utf-8 -*-
# Date: ${YEAR}/${MONTH}/${DAY}
# Filename: ${NAME} 
# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__ = '$USER'
__date__ ="${YEAR}/${MONTH}/${DAY}"

if __name__ == "__main__":
    print "Hello World";