zoukankan      html  css  js  c++  java
  • 网络编程之urllib

    #网络爬虫,从其他的网站上,获取一些有用的内容,存入自己的数据库,然后再展示在指定的位置。
    #urllib是python自带的模块

    1.urllib模块做网络爬虫,爬取网页:
    from urllib import request,parse   #导入urllib模块
    url = 'http://www.nnzhp.cn'
    req = request.urlopen(url) #打开一个url,发get请求
    content = req.read().decode() #获取返回结果
    fw = open('baidu.html','w',encoding='utf-8')
    fw.write(content) #将baidu.html保存在了本地

    2.urllib模块访问接口,get请求:
    import json
    url='http://api.**xx.cn/api/user/stu_info?stu_name=xxxx'
    req = request.urlopen(url) #打开一个url,发get请求
    content = req.read().decode() #获取返回结果
    res_dic = json.loads(content) #返回的结果转成字典
    if res_dic.get('error_code') == 0:
      print('测试通过')
    else:
      print('测试失败',res_dic)

    3.urllib模块访问接口,post请求:

    url = 'http://api.xx**.cn/api/user/login'
    data = {
    'username':'admin',
    'passwd':'xxxx126'
      } #请求数据
    data = parse.urlencode(data) #urlencode,自动给你拼好参数
    # xx=xx&xx=11
    req = request.urlopen(url,data.encode()) #发post请求
    print(req.read().decode())


  • 相关阅读:
    图片水印(转)
    关于confirm无效的一点记录
    简单blog
    wcf入门示例
    树节点查询
    js cookie
    linux 下 ElasticSearch 和 Logstash 安装
    Elasticsearch 如何快速开放外网访问实战
    利用logstash 把mysql 查询的数据定时自动导入ES (超简版)
    Flash闪屏的问题
  • 原文地址:https://www.cnblogs.com/fancyl/p/9041633.html
Copyright © 2011-2022 走看看