[[FrontPage/Python/Matplotlib]]

** WEBサイト内のパスを相対パス⇒絶対パスに変更する方法。 [#x22fe136]
http://blog.livedoor.jp/kmiwa_project/archives/1030405297.html

import urllib2
import lxml.html
 import urllib2
 import lxml.html

html = urllib2.urlopen("http://www/autoproject.nagoya/index.php").read()
dom = lxml.html.fromstring(html)
dom.make_links_absolute("http://www/autoproject.nagoya")
urlList = dom.xpath("//@href")
 html = urllib2.urlopen("http://www/autoproject.nagoya/index.php").read()
 dom = lxml.html.fromstring(html)
 dom.make_links_absolute("http://www/autoproject.nagoya")
 urlList = dom.xpath("//@href")


このlxmlモジュールやxpathの使い方は、こちらのブログが大変参考になります。

http://www.cafe-gentle.jp/challenge/tips/python_tips_001.html
http://www.cafe-gentle.jp/challenge/tips/python_tips_003.html

 

pytyhonでスクレイピングをするのに、pyquery を使ってたけど、 lxmlもシンプルで使いやすい。
何より、相対パスを絶対パスに変換できるの簡単だから素晴らしい。

** [urllib2]Webページ内の画像をダウンロードするPythonスクリプト [#t432c741]
正規表現を使って取得
http://geektrainee.hatenablog.jp/entry/2013/12/15/002640

urllib2だけで取得
http://fififactory.com/2015/03/16/python-%E7%94%BB%E5%83%8F%E3%82%92%E8%87%AA%E5%8B%95%E3%83%80%E3%82%A6%E3%83%B3%E3%83%AD%E3%83%BC%E3%83%89%E3%81%99%E3%82%8B%E3%82%B3%E3%83%BC%E3%83%89-urllib2/

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS