zoukankan      html  css  js  c++  java
  • python爬虫urllib库使用

    urllib包括以下四个模块:

      1.request:基本的HTTP请求模块,可以用来模拟发送请求。就像在浏览器里输入网址然后回车一样,只需要给库方法传入URL以及额外的参数,就可以模拟实现这个过程。

      2.error:异常处理模块

      3.parse:提供了许多URL处理方法,如拆分、解析、合并等

      4.robotparser:主要用来识别网站的robots.txt文件,判断哪些网站可以爬(很少用)

    1.1发送请求

      1urlopen()

    import urllib.request
    response = urllib.request.urlopen('https://baike.baidu.com/item/csdn/172150?fr=aladdin')
    print(response.read().decode('UTF-8')) #read()返回网页内容

    结果:

    #查看返回类型
    import
    urllib.request response = urllib.request.urlopen('https://baike.baidu.com/item/csdn/172150?fr=aladdin') print(type(response))

    status属性

    import urllib.request
    response = urllib.request.urlopen('https://baike.baidu.com/item/csdn/172150?fr=aladdin')
    print(response.status)
    print(response.getheaders())
    print(response.getheader('Server'))

     data参数

      data参数是可选的,如果要添加该参数

  • 相关阅读:
    利用python将表格中的汉字转化为拼音
    vi中批量加注释
    Xtrabackup
    mydumper下载安装
    Adobe Acrobat Pro DC破解
    InnoDB关键特性之double write
    聚集索引与非聚集索引
    has the wrong structure
    初学者如何理解网络协议
    电脑重装系统之后,删除之前的系统
  • 原文地址:https://www.cnblogs.com/alex-xxc/p/9914831.html
Copyright © 2011-2022 走看看