python3网络爬虫开发第三章基本库的使用(2)

本文主要介绍了Python3网络爬虫中request库的使用,包括Request对象的构造、add_header方法添加headers,以及高级用法如Handler和openerDirector。通过Handler和opener,可以实现登录验证、cookie管理和代理设置等功能,为复杂网络请求提供解决方案。
摘要由CSDN通过智能技术生成

3.2 request

Urlopen()可以实现简单的基本请求的发起,但几个简单的参数并不足以完成构建一个完整的请求。如果请求中加入headers等信息,可以利用更加强大的request来构建。

import urllib.request
request=urllib.request.Request('https://www.python.org')
response=urllib.request.urlopen(request)
print(response.read().decode('utf-8'))

<!--[if IE 8]>      <html class="no-js ie8 lt-ie9">                 <![endif]-->
<!--[if gt IE 8]><!--><html class="no-js" lang="en" dir="ltr">  <!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <link rel="prefetch" href="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">

    <meta name="application-name" content="Python.org">
    <meta name="msapplication-tooltip" content="The official home of the Python Programming Language">
    <meta name="apple-mobile-web-app-title" content="Python.org">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="HandheldFriendly" content="True">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="cleartype" content="on">。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
输出内容省略。。。。。。。

我们依然使用urlopen()方法发送这个请求,只不过这次我们先见建立了一个request对象传递给了rasponse,

原因:将请求独立成一个对象,并且方便的配置参数

1) request 对象的构造

1class  urllib.request.Request(url,data=None,headers={},origin_req_ho

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值