爬虫——利用request来post博客园和百度
博客园:
#! /usr/bin/env python
#coding:utf-8
import sys
import re
import urllib2
import urllib
import requests
import cookielib
url='http://httpbin.org/post'
payload = {"yoyoketang":"lalala",
"python":"hello"}
r = requests.post(url, data=payload)
print r.text
print r
百度:
#coding:utf-8
import requests
import json,time
url = "https://passport.baidu.com/"
headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate",
"Referer": "https://passport.baidu.com/v2/?login",
"Cookie":# 此处省略
"Connection": "keep-alive",
"Host":"passport.baidu.com"
}
payload={"input1":"9681BD43724DDB023260051E3B9520A027631E5E200675AAB90D8322FC4935DAB06DFB10D3C4A4C9EC65F589C6A0EE505F001408CFFDCCAC6B7962E4B7E 9908685474397591935B1AEB2AADCDBEFDFE05AE16AB1",
"input2":"CfDJ8Gf3jjv4cttDnEy2UYRcGZ3bsShXX0DA1mccKNUsjZHITE6cjgfFXlPLtU87HDk5TTW2FVwdTjVFcRXuWXJ7VfvjW4m7xFXQlhPkPPwP",
"remember":True}
r = requests.post(url, data=payload, headers=headers,verify=False)
time.sleep(3)
print r.text
print 'code:',r