zoukankan      html  css  js  c++  java
  • python获取网页信息的三种方法

    import urllib.request
    import http.cookiejar
    
    url = 'http://www.baidu.com/'
    
    # 方法一
    print('方法一')
    req_one = urllib.request.Request(url)
    req_one.add_header('User-Agent', 'Mozilla/6.0')
    res_one = urllib.request.urlopen(req_one)
    code_one = res_one.getcode()
    html_one = res_one.read().decode('utf-8')
    res_one.close()
    print('方法一网页状态码:%s' % (code_one))
    print('方法一网页内容:'+html_one)
    
    
    # 方法二
    print('方法二')
    res_two = urllib.request.urlopen(url)
    code_two = res_two.getcode()
    html_two = res_two.read().decode('utf-8')
    print('方法二网页状态码:%s' % (code_two))
    print('方法二网页内容:'+html_two)
    
    
    #方法三
    print('方法三')
    cj = http.cookiejar.LWPCookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    urllib.request.install_opener(opener)
    res_three = urllib.request.urlopen(url)
    print(cj)
    code_three = res_three.getcode()
    html_three = res_three.read().decode('utf-8')
    res_three.close()
    print('方法三网页状态码:%s' % (code_three))
    print('方法三的网页内容:'+html_three)
    
  • 相关阅读:
    C语言面试题——寻找错误
    C语言的声明解释的在线工具——cdecl
    C语言面试题——指针运算
    const 指针与指向const的指针
    C语言复杂声明解释
    poj1248
    poj1750
    poj1484
    poj1853
    poj1575
  • 原文地址:https://www.cnblogs.com/2016-10-07/p/7988368.html
Copyright © 2011-2022 走看看