python 爬虫 教程(1)
来源:互联网 发布:java迭代器是什么 编辑:程序博客网 时间:2024/04/29 06:10
- urllib2
import urllib2response = urllib2.urlopen("http://www.baidu.com")print response.read()
调用的是urllib2库里面的urlopen方法,urlopen一般接受三个参数,它的参数如下:
urlopen(url, data, timeout)
第一个参数url即为URL,第二个参数data是访问URL时要传送的数据,第三个timeout是设置超时时间。
如果第二个参数data为空那么要特别指定是timeout是多少,写明形参,如果data已经传入,则不必声明
import urllib2response = urllib2.urlopen('http://www.baidu.com', timeout=10)
import urllib2response = urllib2.urlopen('http://www.baidu.com',data, 10)
- Request
urlopen参数可以传入一个request请求,其实就是一个Request类的实例,构造时需要传入Url,Data等等的内容。
import urllib2request = urllib2.Request("http://www.baidu.com")response = urllib2.urlopen(request)print response.read()
3.POST和GET与数据
import urllibimport urllib2values = {"username":"1016903103@qq.com","password":"XXXX"}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()
定义一个字典,名字为values,参数我设置了username和password,下面利用urllib的urlencode方法将字典编码,命名为data,构建request时传入两个参数,url和data,运行程序,返回的便是POST后呈现的页面内容。
import urllibimport urllib2values={}values['username'] = "1016903103@qq.com"values['password']="XXXX"data = urllib.urlencode(values) url = "http://passport.csdn.net/account/login"geturl = url + "?"+datarequest = urllib2.Request(geturl)response = urllib2.urlopen(request)print response.read()
get方式直接url+参数就行
0 0
- python 爬虫 教程(1)
- Python 爬虫教程 1 - 前言
- python 爬虫 教程(2)
- python 爬虫 教程(3)
- python爬虫教程大全
- python爬虫教程大全
- python爬虫教程大全
- python爬虫教程大全
- 【python】爬虫教程大全
- python 爬虫教程
- python爬虫教程大全
- python爬虫教程大全
- python爬虫教程大全
- Python爬虫教程大全
- python爬虫教程大全
- Python 爬虫系列教程
- Python 爬虫教程 4
- python 爬虫教程
- 程序员的自述
- Java基础学习之画板1
- git入门
- 即将面对就业问题
- 网页广告添加边框运动
- python 爬虫 教程(1)
- Turn on the program
- Hibernate的核心对象关系映射
- 153. Find Minimum in Rotated Sorted Array 题解
- Java异常体系结构
- 顺时针打印二维数组(螺旋输出数组)
- JavaSE 学习参考:成员内部类
- 杭电 1577 WisKey的眼神
- (李兴华)【FANUC FOCAS1/2 Library 开发系列教程】-序言