标签:etc 数据 headers 操作方法 ons windows 内容 webkit urllib2
# -*- 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
标签:etc 数据 headers 操作方法 ons windows 内容 webkit urllib2
原文地址:http://www.cnblogs.com/zhoujingguoguo/p/7226005.html