爬虫学习--2.urllib 库

urllib了解

urllib 库 是 Python 内置的 HTTP 请求库。urllib 模块提供的上层接口,使访问 www 和 ftp 上的数据就像访问本地文件一样。 有以下几种模块:

  • urllib.request 请求模块

  • urllib.error 异常处理模块

  • urllib.parse url解析模块

  • urllib.robotparser robots.txt 解析模块

urllib.request模块详讲

urllib.request 模块提供了最基本的构造 HTTP 请求的方法,利用它可以模拟浏览器的一个请求发起过程,同时它还带有处理 authenticaton (授权验证), redirections (重定向), cookies (浏览器Cookies)以及其它内容。

常用的方法

  • urllib.request.urlopen("网址"/"请求对象")  作用 :向网站发起一个请求并获取响应 urlopen()不支持重构User-Agent

  • read()  读取服务器响应的内容

  • 字节流 = response.read()

  • 字符串 = response.read().decode("utf-8")

  • getcode() 返回HTTP的响应码

  • geturl()  返回实际数据的URL(防止重定向问题)<

相关推荐

  1. 爬虫学习--2.urllib

    2024-05-02 18:38:01       26 阅读
  2. Python爬虫urllib

    2024-05-02 18:38:01       31 阅读
  3. 爬虫基本的使用(urllib的详细解析)

    2024-05-02 18:38:01       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-02 18:38:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-02 18:38:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-02 18:38:01       82 阅读
  4. Python语言-面向对象

    2024-05-02 18:38:01       91 阅读

热门阅读

  1. 《21天学通C++》(第十三章) 类型转换运算符

    2024-05-02 18:38:01       29 阅读
  2. MyBatis:mybatis入门

    2024-05-02 18:38:01       27 阅读
  3. MySQL:多表查询和事务

    2024-05-02 18:38:01       34 阅读
  4. 程序员副业可用的四大原则

    2024-05-02 18:38:01       29 阅读
  5. Circuits--Sequential--Finite5

    2024-05-02 18:38:01       30 阅读
  6. BKPUNIX

    2024-05-02 18:38:01       28 阅读
  7. 单片机与Python串口通讯

    2024-05-02 18:38:01       30 阅读
  8. Python Json数据解析

    2024-05-02 18:38:01       31 阅读
  9. 单机多GPU的训练及debug中vscode下launch.json内容设置

    2024-05-02 18:38:01       31 阅读