Pythone爬虫学习
入门案例
首先确定好请求对象
import requests #导入requests包
url ='http://www.baidu.com'
strhtml = requests.get(url) #Get方式获取网页数据
print(strhtml.text) #strhtml 是url对象,但是使用.text就表示是网页源码
urlilb2.rulopen()
response =urllib2.rulopen(url,data,timeout)
- url 即URL 必须填
- data,默认为None
- timeiout 超时时间 默认为socket_GLOBAL_DEFAULT_TIMEOUT
执行这个urlopen方法之后,返回的是一个response对象,返回的信息都保存在这里面
response有一个read方法,可以获取到网页的内容
其实url还可以被封装在request中 例如:request=urllib2.Request(url)
再将reqeuest作为参数放到rulopen方法中
推荐使用这种方式,因为在构建请求的时候i还需要加入很多内容,通过构建一个request,服务器响应请求得到应答,这样才显得逻辑清晰
POST和GET数据传输
#`post`
import urllib
import urllib2
values={
"username":"[email protected]","password":"xxx"}
data=urllib.urlencode(values)
url="http://xxxxx"
request =urllib2.Request(url,data)
response=urllib2.urlopen(reqest)
print response.read()
#get
values=[]
values['usernaem']="[email protected]"
values['password']="xxxx"
data=urllib.urlencode(values)
url="http://xxxxx"
geturl