• urllib模块


    ...

    一、urllib请求库
    包括以下模块:
    urllib.request 请求模块
    urllib.error 异常处理模块
    urllib.parse url解析模块
    urllib.ronotparser robots.txt模块



    1.urllib.request.urlopen 方法.
    (1) 参数介绍:
    urlib.request.urlopen(url,
    data=None,
    [timeout,]*,
    cafile=None,
    capath=None,
    cadefault=False,
    context=None)
    常用参数urllib.urlopen(url,data,timeout) # 打开后用response.read()获取到网页中的内容
    1.data 参数:
    bytes(urlib.parse.urlencode()) 可以将post请求数据放到data参数中,请求时带着data请求,
    这样就完成了一次post请求
    如果post请求没有data参数就可以看做一次get请求
    2.timeout参数:
    如果网络很慢,或者请求异常 timeout参数可以设置一个超时时间,而不是让请求一直等下去.
    需要异常抓取
    (2) 响应: response
    response.status #获取状态码
    response.getheaders() #获取头部信息
    response.read()# 获取响应体的内容
    2.request
    设置request
    1. 有很多网站为了防止爬虫造成网站瘫痪,会需要携带一些headers头部信息才能访问.
    最常见的有user-agent参数

  • 相关阅读:
    Ubuntu下 实现Linux与Windows的互相复制与粘贴
    bzoj2426
    bzoj1835
    bzoj1197
    bzoj1049
    bzoj2893
    bzoj1820
    bzoj1819
    bzoj1455
    bzoj3689
  • 原文地址:https://www.cnblogs.com/Treasuremy/p/9636131.html
Copyright © 2020-2023  润新知