zoukankan      html  css  js  c++  java
  • python3爬虫 url管理器

    import urllib.request   #python3中将urllib2拆分为了urllib.request、urllib.error、urllib.response等
    import http.cookiejar
    
    url = "http://www.baidu.com"
    
    print("第一种方法")
    response1 = urllib.request.urlopen(url)
    print(response1.getcode())  #打印response1的状态码看是否请求成功, 200表示请求成功
    print(len(response1.read())) #打印返回网页内容长度
    
    print("第二种方法")
    request = urllib.request.Request(url)
    request.add_header("user-agent", "Mozilla/5.0") #模拟浏览器访问
    request2 = urllib.request.urlopen(request)
    print(request2.getcode())
    print(len(request2.read()))
    
    print("第三种方法")
    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) #在请求对象中添加cookie
    urllib.request.install_opener(opener)
    request3 = urllib.request.urlopen(url)
    print(request3.getcode())
    print(len(request3.read()))
  • 相关阅读:
    JSP文件上传下载组件(2)
    JSP文件上传下载组件(1)
    FetchProfile类的作用
    html(二) -- 文本标签和实体字符
    html (一)-- 概述
    单元测试框架--junit
    内省和BeanUtils
    工厂模式
    反射
    观察者设计模式
  • 原文地址:https://www.cnblogs.com/lsy-ai/p/5666163.html
Copyright © 2011-2022 走看看