2013年6月29日 星期六

[Python] 使用 BeautifulSoup 處理 HTML 程式碼 (HTML Parser) @ Mac 10.8

繼上篇撈一些 HTML 資料回來,接著想要取出 link 來分析,則使用 python BeautifulSoup 套件。

$ sudo port install py27-beautifulsoup

片段程式碼:

from BeautifulSoup import BeautifulSoup
import urllib, urllib2

url = "http://blog.changyy.org"
data = urllib2.urlopen(url)
soup = BeautifulSoup(data)

for article in soup.findAll("div", {'class':'item-title'}):
    print "Title: " + article.a.contents[0]

    print "Link: " + article.a['href']
    print ""


運行結果:

$ python2.7 /tmp/t.py
Title: [Linux] 使用 sed 更新 PATH 環境變數 @ Ubuntu 12.04
Link: http://blog.changyy.org/2013/06/linux-sed-path-ubuntu-1204.html

Title: 致我們終將失去的熱情
Link: http://blog.changyy.org/2013/06/blog-post.html

Title: [Linux] 透過 busybox nc 提供簡易 socket server/client 測試方式
Link: http://blog.changyy.org/2013/06/linux-busybox-nc-socket-serverclient.html

Title: [Linux] 查看主機板型號 @ Ubuntu 12.04
Link: http://blog.changyy.org/2013/06/linux-ubuntu-1204.html

Title: blog.changyy.org 正式啓用
Link: http://blog.changyy.org/2013/06/blogchangyyorg.html

Title: [PHP] 使用 Heroku 架設 Wordpress 免費部落格
Link: http://blog.changyy.org/2013/05/php-heroku-wordpress.html

Title: [Python] 備份 Github.com 上的資料(可指定 user)@ Mac 10.8
Link: http://blog.changyy.org/2013/06/python-githubcom-user-mac-108.html

Title: 暮色、好星晴
Link: http://blog.changyy.org/2013/06/blog-post_24.html

Title: [Raspberry Pi] 使用 Raspbmc 之 AirPlay 播放影音 @ Mac 10.8
Link: http://blog.changyy.org/2013/06/raspberry-pi-raspbmc-airplay-mac-108.html

Title: Android 開發筆記 - 使用 mDNS 偵測裝置與 TXT record 處理方式 @ Mac 10.8
Link: http://blog.changyy.org/2013/05/android-mdns-txt-record-mac-108.html

[Python] 使用 zlib 處理 gzip 資料,以 curl 與 web 資料為例 @ Mac 10.8

抽空復習一下 Python ,想到用 Python 撈資料。

這時就先用了 pycurl 去跟 Web server 要資料,接著嘗試跟 Web server 要 gz 形態的資料回來,並使用 zlib 解掉。

範例:

import pycurl
import StringIO
import zlib

def getWebData(url):
    c = pycurl.Curl()
    c.setopt( pycurl.URL , url.encode('utf-8') )
    c.setopt( pycurl.FOLLOWLOCATION , True )
    c.setopt( pycurl.HTTPHEADER , [
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0',
        'Accept-Language: zh-tw,zh;q=0.8,en-us;q=0.5,en;q=0.3',
        'Accept-Encoding: gzip, deflate',
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    ])

    open('/tmp/pycurl','wb').close()
    c.setopt( pycurl.COOKIEFILE , '/tmp/pycurl' )
    c.setopt( pycurl.COOKIEJAR , '/tmp/pycurl' )

    b = StringIO.StringIO()
    h = StringIO.StringIO()

    c.setopt(pycurl.WRITEFUNCTION, b.write)
    c.setopt(pycurl.HEADERFUNCTION, h.write)

    c.perform()
    
    body = b.getvalue()
    b.close()
    header = h.getvalue()
    h.close()
    
    return {'header':header, 'body':body}


target = "http://blog.changyy.org"
#data = urllib2.urlopen(target)
data = zlib.decompress(getData(target)['body'], 16+zlib.MAX_WBITS)

print data


透過 pycurl 的 request header 指定 gzip 形態,收回來後,在用 zlib.decompress 解開來使用。

2013年6月26日 星期三

[Linux] 透過 busybox nc 提供簡易 socket server/client 測試方式

最近在整合嵌入式的環境,碰到 socket server/client 的模式,正在想有沒合適的測試方式,就發現了 busbox nc 的用法!在嵌入式環境中真的是超方便的。

$ busybox nc
BusyBox v1.18.5 (Ubuntu 1:1.18.5-1ubuntu4.1) multi-call binary.

Usage: nc [-iN] [-wN] [-l] [-p PORT] [-f FILE|IPADDR PORT] [-e PROG]

Open a pipe to IP:PORT or FILE

Options:
-e PROG Run PROG after connect
-l Listen mode, for inbound connects
(use -l twice with -e for persistent server)
-p PORT Local port
-w SEC Timeout for connect
-i SEC Delay interval for lines sent
-f FILE Use file (ala /dev/ttyS0) instead of network

簡言之,若是整合的程式是一隻 socket server,要走 socket 跟他溝通,這時就可以用 nc 指令測試,假設 server 跑在 port 55688,且 server 是 read 一段話後,去判斷那段話來做事,例如 play boy 好了,那這時只需用 nc 就可以模擬 socket 連線,可省去寫一段小程式來測驗:

$ echo "play boy" | busybox nc server_ip server_port

除了上述 client mode 外,其實 nc 也能當 server:

$ busybox nc -l -p server_port

能夠有這樣的測試方式真是太幸福了 XD

修正 ARM device hostname 為 (none) 問題

hostname (None)
手上有台裝置是 ARM-based Linux device,在其上頭執行 busybox hostname 的結果是 (none) ,原先以為只要更新 /etc/hostsname 後即可改善,但結果仍是不行的。

最後的解法是更新 /proc/sys/kernel/hostname 來處理,由於此例 (none) 的影響某些網路程式的判斷,主要是不 unique 的影響,故手動處理一下,其片段程式:

# update /proc/sys/kernel/hostname
checkhostname=`grep -i -c "(None)" /proc/sys/kernel/hostname`
if [ $checkhostname -gt 0 ]; then                        
newhostname=n$(echo $macaddress | sed 's/://g')            
echo $newhostname > /proc/sys/kernel/hostname        
fi

2013年6月25日 星期二

blog.changyy.org 正式啓用

byebye

前陣子下了決定買了 changyy.org,覺得在資訊業打滾了一會兒後,應該多多建立個人品牌,除了持續將恰當的東西放在 github.com/changyy 外,再買了一個 domain name 來用用,一年也不貴,在 www.namecheap.com 約 10 塊美金,說起來還比一些台灣 .tw 系列的便宜 Orz 真不知該說什麼...連 .com 也是這等價

接著,以為用了多年的部落格只需買一下一般 VIP 會員就能設定使用自己的 domain name ,事實證明我錯了("個人化網址"不是這個意思) Orz 基於需求的不滿,我就試用退費流程(慶幸用的是信用卡,退費無痛),便開始認真地思考搬家議題,把原先一直同步備份在 Blogger 的網站公開了,沒想到就這樣正式啟用了 XD

就這樣,要來適應新的文章撰寫環境了,至於程式碼的部分會在試看看,也可能大部份都擺在 github.com/changyy 吧!

雖然 Google Reader 剩幾天就收了,但這邊還是提一下,原本 Blogger 提供的訂閱位址,在 Google Reader 裡排版都會亂掉且沒法顯示圖片,所以我還是申請了 feeds.feedburner.com/changyyorg 來用用,還有興趣的可以改訂閱這邊,但有三分之一都在碎碎念!

至於部落格廣告嘛,就當作從搜尋引擎過來查資料的過路費吧 XDDD 可以拿來支援買 domain name 囉!