zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    bzoj 3243: [Noi2013]向量内积
    bzoj 4818: [Sdoi2017]序列计数
    AtCoder Grand Contest 023 F
    bzoj 4573: [Zjoi2016]大森林
    bzoj 5305: [Haoi2018]苹果树
    bzoj 5298: [Cqoi2018]交错序列
    codeforces496C
    codeforces534B
    牛客小白月赛13
    codeforces605A
  • 原文地址:https://www.cnblogs.com/guanfuchang/p/6794639.html
Copyright © 2011-2022 走看看