Python 2.7获取网站源代码的几种方式_20160924
2024-08-29 03:33:20
#coding:utf-8
import urllib2,cookielib if __name__ == '__main__':
root_url='https://www.baidu.com/'
# 第一种
print "第一种"
response1=urllib2.urlopen(root_url)
print response1.getcode()
print len(response1.read()) #第二种
print "第二种"
request=urllib2.Request(root_url)
response2=urllib2.urlopen(request)
print response2.getcode()
print len(response2.read()) # 第三种
print "第三种"
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(root_url)
print response3.getcode()
print "cookie为:"+str(cj)
print response3.read()
执行结果
最新文章
- JavaScript中JSON的处理心得
- java获取当前执行文件的路径
- NGUI例子Scroll View场景中item添加点击后自动滑到终点
- uva331 - Mapping the Swaps
- angularjs的一些优化小技巧
- 在Android Studio中进行单元测试和UI测试
- hdu2102(bfs)
- 455. Assign Cookies.md
- 智能指针std::weak_ptr
- 『2019/3/8 USACO测试 反思与总结』
- npm常用命令学习(npm install -D,semver版本规范, npm进行版本管理的最佳实践用法)
- Git push 时如何避免出现 ";Merge branch 'master' of ...";
- Androi:ViewPager
- 使用super调用父类的构造方法
- Linux系统如何模拟Http的get或post请求?
- Android之listview运用(美团美食列表)
- Python学习第一弹
- C#简单的文件阅读器
- iptables 使用场景
- iOS ZipArchive文件解压缩