美文网首页
requests 模块实现小爬虫

requests 模块实现小爬虫

作者: 夜醉梦紅尘 | 来源:发表于2019-09-30 13:58 被阅读0次

    requests
    安装
    pip3 install requests
    官方中文文档:http://cn.python-requests.org/zh_CN/latest/user/quickstart.html#id4
    get

    # 1、无参数实例
      
    import requests
      
    ret = requests.get('https://github.com/timeline.json')
      
    print(ret.url)
    print(ret.text)
      
      
    # 2、有参数实例
      
    import requests
      
    payload = {'key1': 'value1', 'key2': 'value2'}
    ret = requests.get("http://httpbin.org/get", params=payload)
      
    print(ret.url)
    print(ret.text)
    

    POST

    # 1、基本POST实例
      
    import requests
      
    payload = {'key1': 'value1', 'key2': 'value2'}
    ret = requests.post("http://httpbin.org/post", data=payload)
      
    print(ret.text)
      
      
    # 2、发送请求头和数据实例
      
    import requests
    import json
      
    url = 'https://api.github.com/some/endpoint'
    payload = {'some': 'data'}
    headers = {'content-type': 'application/json'}
      
    ret = requests.post(url, 
                        data=json.dumps(payload), 
                        headers=headers)
    
    print(ret.text)
    print(ret.cookies)
    
    # 上面的等同于
    ret = requests.post(url, json=payload)
    
    

    相关文章

      网友评论

          本文标题:requests 模块实现小爬虫

          本文链接:https://www.haomeiwen.com/subject/nlqsyctx.html