Python——urllib库的基本使用
来源:互联网 发布:合肥行知学校是重点吗 编辑:程序博客网 时间:2024/06/03 06:52
# coding=utf-8# Urllib库的基本使用import urllibimport urllib2# 1 读取网页内容:先使用urllib2.urlopen打开网址,再read读取html = 'http://cuiqingcai.com/947.html'response = urllib2.urlopen(html, )# print response.read()"""Urllib2.urlopen(url ,data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, cafile=None, capath=None, cadefault=False, context=None)url 1.可以是要访问的网址 2.也可以是由网址构成的Request对象 建议优先使用第2种方法data 访问url时要传送的数据timeout 设置超时时间举个栗子:a = urllib2.urlopen('http://www.baidu.com', data=None, timeout=10).read()b = urllib2.urlopen(urllib2.Request('http://www.baidu.com'), data=None, timeout=10).read()print aprint b"""# 2 读取动态网页内容:动态传送参数数据"""传输参数有两种方法一、post举个栗子:values = {'username': '635943647@qq.com', 'password': '*****'}data = urllib.urlencode(values)url = 'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn'request = urllib2.Request(url, data)response = urllib2.urlopen(request)print response.read()二、get get直接以链接方式访问,链接中包含了所有的参数,如果链接是用来登录则包括你的登录的账号和密码,是一种 比较直观但是不安全的方法举个栗子:values = {'username': '635943647@qq.com', 'password': '*****'}data = urllib.urlencode(values)url = 'http://passport.csdn.net/account/login'geturl = url + data # geturl=http://passport.csdn.net/account/login/username=635943647%40qq.com&password=%2A%2A%2A%2A%2Arequest = urllib2.Request(geturl)response = urllib2.urlopen(request)print response.read()"""values = {'username': '635943647@qq.com', 'password': '*****'}data = urllib.urlencode(values) # data='username=635943647%40qq.com&password=%2A%2A%2A%2A%2A'url = 'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn'request = urllib2.Request(url, data)response = urllib2.urlopen(request)print response.read()
0 0
- Python——urllib库的基本使用
- python之urllib库的基本使用
- Python爬虫教程——入门三之Urllib库的基本使用
- Urllib库的基本使用
- Python爬虫入门三之Urllib库的基本使用
- Python爬虫入门(3):Urllib库的基本使用
- Python爬虫入门(3):Urllib库的基本使用
- Python爬虫入门三之Urllib库的基本使用
- Python爬虫入门三之Urllib库的基本使用
- Python爬虫入门一之Urllib库的基本使用
- Python爬虫入门三之Urllib库的基本使用
- python 网络爬虫入门-Urllib库的基本使用
- Python网络数据采集Urllib库的基本使用
- Python爬虫入门三之Urllib库的基本使用
- python爬虫入门三之Urllib库的基本使用
- Python爬虫入门之Urllib库的基本使用 (三)
- Python爬虫入门三之Urllib库的基本使用
- Python爬虫入门三之Urllib库的基本使用
- 深入浅出设计模式之装饰者设计模式
- hdu3394Railway 双连通分量
- javascript 高级程序设计读书笔记 String函数
- (欺骗了所有人的一张图)图像处理经典图片Lena背后的故事
- jquery validate实现表单验证 (正则表达式)
- Python——urllib库的基本使用
- URAL 1057 数位DP
- SODBASE运维----常见问题解决
- Leetcode - Array - 1. Two Sum (水题,O[n]和O[n^2]实现)
- JVM参数
- 用tensorflow构建线性回归模型
- IOS 多线程技术
- POJ1860——Currency Exchange
- poj 3080 Blue Jeans