Skip to content

Instantly share code, notes, and snippets.

@zhiyue
Created March 25, 2015 06:34
Show Gist options
  • Save zhiyue/b556482784e964323ead to your computer and use it in GitHub Desktop.
Save zhiyue/b556482784e964323ead to your computer and use it in GitHub Desktop.
使用requests登录V2EX,可用于抓取,签到等功能。
# coding=utf-8
import requests
from bs4 import BeautifulSoup as bs
s = requests.Session()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Origin': 'http://www.v2ex.com',
'Referer': 'http://www.v2ex.com/signin',
'Host': 'www.v2ex.com',
}
r = s.get('http://www.v2ex.com/signin', headers=headers)
soup = bs(r.content)
once = soup.find('input', {'name': 'once'})['value']
login_data = {'u': '***', 'p': '***', 'once': once, 'next': '/'}
s.post('http://www.v2ex.com/signin', login_data, headers=headers)
f = s.get('http://www.v2ex.com/settings', headers=headers)
print f.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment