zoukankan      html  css  js  c++  java
  • 获取网页源代码

    # -*- coding: utf-8 -*-
    
    import urllib2
    
    #urllib2 默认的User-Agent是 Python-urllib/2.7
    #User-Agent是爬虫和发爬虫斗争的第一步
    ua_headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2716.5 Safari/537.36"
    }
    
    #通过urllib2.Request()构造一个请求对象
    request = urllib2.Request("http://www.baidu.com/",headers = ua_headers)
    
    
    #向指定的url地址发送请求,并返回服务器响应的类文件对象
    response = urllib2.urlopen(request)
    
    
    #服务器返回的类文件对象支持Python文件对象的操作方法
    #read()就是读取文件里的全部内容,返回字符串
    html = response.read()
    
    #response:是服务器响应的类文件,除了支持文件操作的方法外,还支持以下常用方法
    
    #返回http的响应码,成功返回200,4服务器页面出错,5服务器问题
    print response.getcode()
    
    #返回返回实际数据的实际url,防止有重定向问题 
    print response.getcode()
    
    #返回服务器响应的http报头
    print response.info()
    
    
    print html
  • 相关阅读:
    实际项目管理-1
    arcengine 错误
    一些视频技术类网站
    winform 组件之dotnetbar10.5.3
    winform 弹框的组件
    一个好的开源网站
    写webservice 注意点
    ww
    js
    瀑布流
  • 原文地址:https://www.cnblogs.com/zhoujingguoguo/p/7226005.html
Copyright © 2011-2022 走看看