Python urllib2 调试
2024-08-25 12:25:40
#!/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
最新文章
- sed 技巧
- VS2013 有效密钥
- linux系统定时任务
- 有关项目上潜在需要的移动端GIS系统源码整理,待后续更新
- Spring Boot 2 Swagger2
- ini,config文件的读取修改
- sql 对一张表进行按照不同条件进行多次统计
- Spring中注解事务方面的问题
- 通过qsort(void * lineptr[], int left, int rifht, int (*comp)(void *, void *))解读指针函数和void指针
- Oracle忘记密码如何重
- HTMLParser使用简介
- Android 5.1.1 源码目录结构
- Android启动过程分析
- char
- 使用ANNdotNET进行情感分析
- C#正则Groups高级使用方法
- 【python】参数中的*args和**kwargs
- UML异步怎么表达
- 刚看完了一本关于javascript的书感觉受益匪浅,原来不懂的东西这么多,想问问怎么成为大神?求教!!!!!!
- linux ncat命令
热门文章
- 30天React Native从零到IOS/Android双平台发布总结
- 背水一战 Windows 10 (13) - 绘图: Stroke, Brush
- php图片验证码为什么必须加上ob_clean();才能正常显示。
- centos下安装php扩展php-memcached
- spring面试题(2)
- OData V4 系列 Action 与 Function
- 两种文件上传的实现-Ajax和form+iframe
- Material Design 概念,环境和基本属性
- ReactiveCocoa代码实践之-更多思考
- IOS开发基础知识--碎片5