LoginSignup
5
6

More than 5 years have passed since last update.

Python スクレイピング関連のメモ

Last updated at Posted at 2018-09-19

Beautiful Soup

インストール

pip install beautifulsoup4

from bs4 import BeautifulSoup

soup = BeautifulSoup(open("./index.html", encoding='UTF-8'), "html.parser")

for link in soup.find_all('a'):
    print(link.get('href'))

正規表現

インポート

import re

文字列の先頭でのみのマッチを確認する

re.match(pattern, string)

文字列内の位置にかかわらずマッチを確認する (Perl でのデフォルトの挙動)

re.search(pattern, string)

判定

match() と search() はマッチしなかった場合に None を返すので、単純な if ステートメントによってマッチしたかどうかをテストできます

match = re.search(pattern, string)
if match:
    process(match)

Data pretty printer

pprint モジュールを使うと、Pythonの任意のデータ構造をインタープリタへの入力で使われる形式にして "pretty-print" できます。

import pprint

stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
stuff.insert(0, stuff[:])
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(stuff)

参考

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6