#!/usr/bin/env python
# coding=utf-8
__author__ = 'zhaoyingnan'
import urllib
import urllib2
import chardet class HtmlLoader:
def urlLoad(self, sUrl, isPost=False, isDebug=False):
try:
sContent = None
dictHeaders = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Charset': 'utf-8',
'Referer': 'https://www.baidu.com/?tn=92765401_hao_pg',
'Accept-Language': 'zh-CN,zh;q=0.8',
} dictData = {}
sData = urllib.urlencode(dictData)
if (isPost is True):
Request = urllib2.Request(url=sUrl, data=sData, headers=dictHeaders)
else:
Request = urllib2.Request(url=sUrl, headers=dictHeaders) # print Request.get_method() # 打印请求类型 # 代理
ProxyHandler = urllib2.ProxyHandler({}) # 调试
httpDebugHandler = urllib2.HTTPHandler(debuglevel=1)
httpsDebugHandler = urllib2.HTTPSHandler(debuglevel=1) if (isDebug is True):
Opener = urllib2.build_opener(ProxyHandler, httpDebugHandler, httpsDebugHandler)
else:
Opener = urllib2.build_opener(ProxyHandler) urllib2.install_opener(Opener)
Response = Opener.open(Request)
iCode = Response.getcode()
if (iCode == 200):
sContent = Response.read() if (sContent is not None):
# print(chardet.detect(sContent))
encoding = chardet.detect(sContent)
if (encoding['encoding'].startswith("GB")):
sContent = sContent.decode("GBK")
return sContent
except Exception as e:
print(e)
finally:
pass if (__name__ == '__main__'):
sUrl = 'http://www.cnblogs.com/yingnan/p/5659966.html'
demo = HtmlLoader()
sContent = demo.urlLoad(sUrl=sUrl, isPost=False, isDebug=True)
print sContent

  

最新文章

  1. sed 技巧
  2. VS2013 有效密钥
  3. linux系统定时任务
  4. 有关项目上潜在需要的移动端GIS系统源码整理,待后续更新
  5. Spring Boot 2 Swagger2
  6. ini,config文件的读取修改
  7. sql 对一张表进行按照不同条件进行多次统计
  8. Spring中注解事务方面的问题
  9. 通过qsort(void * lineptr[], int left, int rifht, int (*comp)(void *, void *))解读指针函数和void指针
  10. Oracle忘记密码如何重
  11. HTMLParser使用简介
  12. Android 5.1.1 源码目录结构
  13. Android启动过程分析
  14. char
  15. 使用ANNdotNET进行情感分析
  16. C#正则Groups高级使用方法
  17. 【python】参数中的*args和**kwargs
  18. UML异步怎么表达
  19. 刚看完了一本关于javascript的书感觉受益匪浅,原来不懂的东西这么多,想问问怎么成为大神?求教!!!!!!
  20. linux ncat命令

热门文章

  1. 30天React Native从零到IOS/Android双平台发布总结
  2. 背水一战 Windows 10 (13) - 绘图: Stroke, Brush
  3. php图片验证码为什么必须加上ob_clean();才能正常显示。
  4. centos下安装php扩展php-memcached
  5. spring面试题(2)
  6. OData V4 系列 Action 与 Function
  7. 两种文件上传的实现-Ajax和form+iframe
  8. Material Design 概念,环境和基本属性
  9. ReactiveCocoa代码实践之-更多思考
  10. IOS开发基础知识--碎片5