Created
February 18, 2018 05:31
Qiitaのログインが必要なページのスクレイピング (2018.2時点)
This file contains 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
``` | |
ログインが必要なQiitaページのスクレイピングをする場合のサンプル。 | |
Qiitaの仕様の問題なので、特別な技術は使っていない。 | |
そして仕様なので、そのうちまた変わると思われる。 | |
``` | |
# ログイン情報 | |
payload = { | |
'utf8': '✓', | |
'identity': QIITA_ID, | |
'password': QIITA_PASS | |
} | |
# urllibとかのopen()ではなく、requestsのSession()でやる | |
s = requests.Session() | |
r = s.get('https://qiita.com') | |
soup = BeautifulSoup(r.text, 'html.parser') | |
# アクセス時に必要なのは、access_tokenではなく, csrf_tokenになっている | |
csrf_token = soup.find(attrs={'name': 'csrf-token'}).get('value') | |
payload['csrf-token'] = csrf_token | |
# 通常のスクレイピングとほぼ同じ。 | |
url = 'https://qiita.com/search?q=' + urllib2.quote(word) | |
html = s.get(url, data=payload).text | |
soup = BeautifulSoup(html, 'html.parser') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
というかAPI使えよっていう話はごもっともなんですよね。