Last active
May 31, 2017 10:28
-
-
Save sakage24/ae36c8a5bc332c7289c2cc8bffb755bb to your computer and use it in GitHub Desktop.
2ch_scraper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
certifi==2017.4.17 | |
chardet==3.0.3 | |
colorama==0.3.9 | |
cssselect==1.0.1 | |
Django==1.11.1 | |
httplib2==0.10.3 | |
idna==2.5 | |
image==1.5.5 | |
lxml==3.7.3 | |
olefile==0.44 | |
Pillow==4.1.1 | |
pytz==2017.2 | |
requests==2.16.0 | |
selenium==3.4.2 | |
urllib3==1.21.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# チョー簡単な使い方です。なんかurllibよりhttplib2使ったほうがいいらしいよ。 | |
# httplib2で接続して、chardetで文字コード取得、取得したデータをlxmlで解析...っていう流れですね。 | |
import httplib2 | |
import lxml.html | |
import chardet | |
# 試しにうちのブログに接続してみる | |
http = httplib2.Http('.cache') | |
response, content = http.request("http://sakage24.hatenablog.jp/entry/2017/05/26/100420") | |
# 文字コード取得 | |
moji = chardet.detect(content) | |
# 取得した文字コードでデコードする | |
str_content = content.decode(moji['encoding']) | |
# lxmlで扱いやすいように変換する | |
root = lxml.html.fromstring(str_content) | |
# pタグの全ての要素を表示する | |
for p in root.cssselect('p'): | |
print(p.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment