通过urllib2抓取网页内容(1)
来源:互联网 发布:质量管理体系数据分析 编辑:程序博客网 时间:2024/06/03 17:37
一、urllib2发送请求
一般html表单,data需要编写成标准的形式,调用urllib.urlencode()进行urlencode编码,编写完然后用问号衔接 加到url后面
import urllib2url = 'http://www.baidu.com'req = urllib2.Request(url)response = urllib2.urlopen(req)print response.read()print response.geturl()print response.info()
urllib2用一个Request对象,来映射HTTP请求,并将这个请求传入urlopen()中去,返回response对象
Request =>Response http就是基于这种请求/应答机制
response对象,是一个文件对象,可以调用read(),info(),geturl()等方法
response.read() 读取返回的内容
response.info()获取返回header
response.geturl() 获取实际访问的url
urllib2使用相同的接口处理所有的URL头,比如可以这样创建一个ftp请求
req = urllib2.Request('ftp://duote.com')
二、POST请求
import urllib2url = "http://www.duote.com/index?php"data = {"softname":"quicktime.exe","size":"18763","md5":"HEN35FLK3WP"}req = urllib2.Request(url,data)response = urllib2.urlopen(req)print response.read()
Request(url,data=None,headers={},orgin_req_host=None,univerifiable=False)
urlopen(url, data=None, timeout=<object object>, cafile=None, capath=None, cadefault=False, context=None)
import urllibimport urllib2url = "http://www.2345.com"data = {'name':'Tom','age':'18','studynum':'002195'}urlvalue = urllib.urlencode(data)print urlvaluer_url = url +'?' +urlvaluedata = urllib2.urlopen(r_url)
一般html表单,data需要编写成标准的形式,调用urllib.urlencode()进行urlencode编码,编写完然后用问号衔接 加到url后面
0 0
- 通过urllib2抓取网页内容(1)
- 利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- TCP处理主要开销
- mark一下
- 【美团技术博客】Hive SQL的编译过程
- 扩展Coroutine:自定义YieldInstruction
- poj3264
- 通过urllib2抓取网页内容(1)
- CVPR2015一些文章整理
- sql2012安装时出现Netfx3相关问题
- 习题1-2 温度(temperature)
- Python3 支持 MySQL
- 2015年7月16号
- 如何跟踪Entity Framework生成的SQL!
- Hibernate初认识以及HelloWorld
- OC_继承,便利构造器,初始化方法