python 爬虫002-http与urllib2

urllib2 GET  

https://www.oschina.net/home/login

#!/usr/bin/env python 
# -*- coding: utf-8 -*-
import urllib2
import sys
type = sys.getfilesystemencoding()

if __name__ == '__main__':
    # 设置Request的url信息和头部信息
    url = "https://www.oschina.net/home/login"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'}
    request = urllib2.Request(url=url, headers=headers)
    # 发送请求和接收响应
    response = urllib2.urlopen(request)
    print response.read().decode("UTF-8").encode(type)

urllib2 POST  

https://www.oschina.net/action/user/hash_login      {'email': 'abc@qq.com', 'pwd': '123456', 'save_login': 1}

#!/usr/bin/env python 
# -*- coding: utf-8 -*-
import urllib2
import urllib
import sys
type = sys.getfilesystemencoding()

if __name__ == '__main__':
    # 设置Request的url信息和头部信息
    url = "https://www.oschina.net/action/user/hash_login"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'}
    values = {'email': 'abc@qq.com', 'pwd': '123456', 'save_login': 1}
    data = urllib.urlencode(values)
    request = urllib2.Request(url=url, data=data, headers=headers)
    # 发送请求和接收响应
    response = urllib2.urlopen(request)
    print response.read().decode("UTF-8").encode(type)

 

posted @ 2017-05-02 09:26  鲨鱼逛大街  阅读(259)  评论(0编辑  收藏  举报