python---urllib2使用

# coding:utf-8
import urllib2
import sys


var_headers = {
    "User-Agent" :
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
}

var_request = urllib2.Request("http://www.biaud.com", headers=var_headers)

var_response = urllib2.urlopen(var_request)

#獲取頁面內容
var_html = var_response.read()

#獲取狀態code
var_html = var_response.getcode()  #200

#返回實際數據的實際URL,防止重定向問題
var_html = var_response.geturl()

#返回response info
var_html = var_response.info()
# Date: Wed, 15 Nov 2017 07:02:04 GMT
# Server: Apache/2.4.29 (Unix)
# X-Powered-By: PHP/5.3.28
# Connection: close
# Transfer-Encoding: chunked
# Content-Type: text/html

print(var_html)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章