More Related Content
Similar to PythonによるWebスクレイピング入門 (20)
PythonによるWebスクレイピング入門
- 3. • 前職まで
• RSSリーダー、SNS
• WebMail
• 写真共有サービス
• 現在(アライドアーキテクツ)
• モニプラFacebook、Social-IN
• Webアプリケーション開発全般を担当
経歴
- 15. サンプル
import re, urllib2
res = urllib2.urlopen( http://ll.jus.or.jp/2014/program")
pattern_title = re.compile( <title>(.*?)</title>')
m = pattern_title.search(res.read())
title = m.group(1)
print title
>>> プログラム ¦ LL Diver
- 17. サンプル
import urllib2
from bs4 import BeautifulSoup
res = urllib2.urlopen( http://ll.jus.or.jp/2014/program )
soup = BeautifulSoup(res.read())
soup.title
>>> <title>プログラム ¦ LL Diver</title>
for link in soup.find_all( a'):
print(link.get( href'))
>>> http://ll.jus.or.jp/2014/
>>> http://ll.jus.or.jp/2014/
- 19. サンプル
from pyquery import PyQuery as pq
d = pq("http://ll.jus.or.jp/2014/program")
print d( title").text()
>>> プログラム ¦ LL Diver
print d( .entry-title").text()
>>> プログラム
print d( #day ).text()
>>> 昼の部
- 34. 手順
• Scrapy プロジェクトの作成
• 抽出するアイテムの定義
• アイテムの抽出とクローリングのためのSpider
を作成
• 抽出したアイテムのパイプライン部分作成
• 基本はSpiderとItem部分を書いていけばOK
- 35. プロジェクト作成
$ scrapy startproject scrapy_sample
$ tree scrapy_sample
scrapy_sample/
├── scrapy.cfg
└── scrapy_sample
├── __init__.py
├── items.py
├── pipelines.py
├── settings.py
└── spiders
└── __init__.py
- 37. Spider作成
class MininovaSpider(CrawlSpider):
name = 'mininova'
allowed_domains = ['mininova.org']
start_urls = ['http://www.mininova.org/yesterday/']
rules = [Rule(LinkExtractor(allow=['/tor/d+']), parse_torrent')]
!
def parse_torrent(self, response):
torrent = TorrentItem()
torrent['url'] = response.url
torrent['name'] = response.xpath("//h1/text()").extract()
return torrent
- 38. 実行
$ scrapy crawl mininova -o scraped_data.json
$ cat scraped_data.json
{
"url": "http://www.mininova.org/tor/13277197",
"name": ["lady anna voice free plugin VSTI free download new by Softrave ]
},
{
"url": "http://www.mininova.org/tor/13277195",
"name": ["mandala VSTI free download new by Softrave ]
},
- 40. 参考URL
• http://scrapy.org ( Scrapy )
• http://www.slideshare.net/MasayukiIsobe/web-scraping-20140622isobe
• https://github.com/gawel/pyquery/ ( pyquery )
• http://www.crummy.com/software/BeautifulSoup/ ( BeautfulSoup )
• http://orangain.hatenablog.com/entry/scrapy
• http://akiniwa.hatenablog.jp/entry/2013/04/15/001411
• http://tokyoscrapper.connpass.com/ ( Webスクレイピング勉強会 )
• http://www.slideshare.net/nezuQ/web-36143026?ref=http://www.slideshare.net/slideshow/
embed_code/36143026
• http://qiita.com/nezuq/items/c5e827e1827e7cb29011( 注意事項 )