美文网首页
Python 网络数据采集1

Python 网络数据采集1

作者: 墨凌风起 | 来源:发表于2020-07-11 16:44 被阅读0次

    _-coding:utf-8--

    '''
    Urllib模块:一个基于Http 访问请求的Python模块库,包括以下:
    1.urllib.request -- 请求模块
    2.urllib.error -- 异常处理模块
    3.urllib.parse -- URL的解析模块
    4.urllib.rebotparser -- robots.txt解析模块
    '''

    1.1.1urllib.request模块

    '''
    作用:向互联网服务器发送请求,并获取网络服务器的反馈数据信息
    urlopen()函数作用是与指定的网络资源简历连接(主要通过URL)
    使用前先import urllib.request
    urllib.request.urlopen(url,data = None[,timeout]),.....
    url:网页地址
    data = None:请求参数数据
    timeout:请求超时时间
    ......:其余参数
    '''

    import urllib.request
    url_req = 'https://www.jianshu.com/p/34b0a6dd1ae0'
    response = urllib.request.urlopen(url_req)
    '''
    response:服务器响应数据对象
    response.geturl():获取请求URL地址
    response.getcode():获取返回码
    response.getinfo():获取网页返回信息
    '''
    print('request的URL= %' % response.geturl)
    print('request的返回码 = {0}'.format(response.getcode))
    print('request的返回信息:{0}'.format(reqponse.getinfo))

    1.2.获取网页数据信息

    '''
    使用response.read()获取指定网页的源代码
    '''
    import os
    disPath = os.path.join(os.getced(),'URLlib/respnse_data')
    if not os.path.exists(disPath):
    os.madir(disPath)
    pass

    with open(os.path.join(disPath,'baidu.html'),'w',encoding='utf-8') as fp:
    fp.write(response.read().decode('utf-8'))#使用utf-8对数据编码

    1.3 携带请求参数

    '''
    urlopen()第二个参数为 data,用于设置请求参数
    格式:字典类型
    '''
    dictParams = {'search_text':'ios开发','id':'1001'}
    data = pickle.dumps(dictParams)#序列化电子流
    response01 = urllib.request.open(url,data = data)
    if response.getcode() == 200:
    #请求到数据结果了
    pass

    1.4超时

    '''
    timeout:服务器未按时返回数据则抛出socket.timeout异常
    '''
    import socket
    try:
    response = urllib.request.urlopen(url_req,timeout = 0.8)
    pass
    except socket.timeout:
    print('>>>连接请求超时')

    1.5网站403/403

    '''
    405/403代表网站拒绝访问
    网站为了防止爬虫,访问是需要携带header -- user-agent
    '''
    header = {'user-agent':'Mozilla/4.0','Host':'httpbin'[,...]}
    req = urllib.request.Request(url = url,header = header,method = "POST")
    response02 = request.urlopen(req)
    '''
    也可以这样:
    req = urllib.request.Request(url = url,header = header)
    req.add_header= header
    '''

    1.6网络资源下载

    '''
    只需要获取图片,音频,视频的二进制资源地址,直接连接资源以二进制写入文件即可
    response.read() 返回字节
    '''

    2.Request模块

    '''
    一个基于urllib模块简单易用的第三方网络HTTP请求库
    采用Apache2 Licensed 开源协议的HTTP库
    '''

    2.1安装配置requests

    '''
    Requests模块需要使用pip下载安装部署:pip install -U requests
    使用时:import requests
    '''
    import requests
    url = 'http://www.baidu.com'

    使用get()发送请求

    response = requests.get(url)

    requests携带参数无需序列化,函数自身已实现

    response = requests.get(url,params={'search_text':'zhang','id':1001})

    扩展:请求提交的六种方法

    '''
    1.requests.get() GET请求
    2.requests.post()....POST请求
    3.requests.put().....PUT请求
    4.requests.delete()..DELETE请求
    5.requests.head()
    6.requests.options()
    '''

    response对象

    '''
    .status_code: #响应状态码
    .raw #返回原始响应体,即urllib的response对象,使用r.raw.read()读取
    .content #字节方式响应体,会自动解码gzip和deflate压缩
    .text #字符串方式响应体,会自动根据响应头部的字符编码进行解码
    .headers #以字典对象储存服务器响应头,该字典特殊,key不区分大小写,若key不存在,返回None
    .json #Request内置json解析器
    .raise_for_status() #请求失败(非200)抛出异常
    '''

    相关文章

      网友评论

          本文标题:Python 网络数据采集1

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