python-网络爬虫初学一:获取网页源码以及发送POST和GET请求
来源:互联网 发布:淘宝联盟认证 编辑:程序博客网 时间:2024/05/18 18:53
一、工具包urlllib和urllib2导入;
# GET和POST请求需要工具包urllibimport urllib# 导入工具包import urllib2
二、a)爬取网站对应的源码
# 通过资源定位符获取网页对象,通过read方法返回网页的源码response = urllib2.urlopen("http://www.baidu.com")print response.read()
b)将其写得规范一点,则如下所示
# 构造request请求实例request = urllib2.Request("http://www.baidu.com")response = urllib2.urlopen(request)print response.read()
三、构造POST请求
# POST请求values = {"username": "geek", "password": "**********"}# 或者values = {}values["username"] = "geek"values["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": "geek", "password": "**********"}data = urllib.urlencode(values)url = "https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn"request = url + "?" + dataresponse = urllib2.urlopen(request)print response.read()
0 0
- python-网络爬虫初学一:获取网页源码以及发送POST和GET请求
- Java爬虫(一)利用GET和POST发送请求,获取服务器返回信息
- Java爬虫(一)利用GET和POST发送请求,获取服务器返回信息
- python网络爬虫学习(一)通过GET和POST方式获取页面内容
- iOS 发送GET和POST网络请求
- Python发送GET和POST请求
- python发送post和get请求
- Python发送get,post请求
- python 使用urllib2发送get请求和post请求
- java 发送post和get请求源码详解
- 【iOS开发-网络】使用NSURLSessionDataTask发送get和post请求
- iOS网络笔记--AFNetWorking发送get和post请求
- HttpClient框架发送get和post网络请求
- python用httplib模块发送get和post请求
- python用httplib模块发送get和post请求
- python用httplib模块发送get和post请求
- python用httplib模块发送get和post请求
- python用httplib模块发送get和post请求
- springboot 学习笔记(一)
- springboot 学习笔记(二)--- properties 配置
- 2017.2.10测试 藏妹子之处
- linux下rsync命令详细整理
- fastJson泛型如何转换
- python-网络爬虫初学一:获取网页源码以及发送POST和GET请求
- codevs 3342绿色通道
- mac 开发必备软件(不断update ing...)
- 可爱的猴子
- Gson 和 Fastjson 你不知道的事
- 八数码难题 洛谷1379
- Sql优化方法
- Mysql tinyint smallint int bigint 区别
- codevs2924 数独挑战