Skip to content

Instantly share code, notes, and snippets.

@karashi39
Created February 18, 2018 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karashi39/a16266981b4154baa605f69c7fb36153 to your computer and use it in GitHub Desktop.
Save karashi39/a16266981b4154baa605f69c7fb36153 to your computer and use it in GitHub Desktop.
Qiitaのログインが必要なページのスクレイピング (2018.2時点)
```
ログインが必要な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')
@karashi39
Copy link
Author

というかAPI使えよっていう話はごもっともなんですよね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment