python 爬虫002-http与urllib2

it2024-08-20  33

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)

 

转载于:https://www.cnblogs.com/guanfuchang/p/6794639.html

最新回复(0)