python爬虫基础知识之requests,读取图片的两只方式,cookies,beautifulsoup
2017-12-12 16:53
846 查看
Requests
1.
o 发送请求
o 传递URL参数
o 读取响应内容(文本/二进制/Json)
o 定制请求头部
o Post请求
o 响应状态码
o 重定向和历史
o 超时
2. 处理二进制内容导入包
#抓取图片
from PIL import Image
#处理二进制内容
from io import BytesIO
3. 图片处理
1) #二进制数据
r=requests.get('http://img.mshishang.com/pics/2016/1209/20161209125708793.jpeg')
image=Image.open(BytesIO(r.content))
image.save('shauige.jpg')
2) #原始数据处理,还是处理图片的例子,利用流处理,效率高
r=requests.get('http://img.mshishang.com/pics/2016/1209/20161209125708793.jpeg',stream= True)
#w表示写,b表示二进制,+表示原来有的话删除掉
while open('shuaige2.jpg','wb+') as f:
for chunk inr.iter_content(1024):
f.write(chunk)
4. 提交表单and cookies and 重定向
#提交表单
form={'username':'user','password':'pass'}
r=requests.get('http://httpbin.org/post',data=form)
print(r.text)
r=requests.post('http://httpbin.org/post',data=json.dumps)
print(r.text)
#cookie
url='http://www.baidu.com'
r=requests.get(url)
cookies=r.cookies
for k, v in cookies.get_dict().items():
print(k,v)
5. Beautiful soup
soup=Beautifulsoup(open(url))
1.
o 发送请求
o 传递URL参数
o 读取响应内容(文本/二进制/Json)
o 定制请求头部
o Post请求
o 响应状态码
o 重定向和历史
o 超时
2. 处理二进制内容导入包
#抓取图片
from PIL import Image
#处理二进制内容
from io import BytesIO
3. 图片处理
1) #二进制数据
r=requests.get('http://img.mshishang.com/pics/2016/1209/20161209125708793.jpeg')
image=Image.open(BytesIO(r.content))
image.save('shauige.jpg')
2) #原始数据处理,还是处理图片的例子,利用流处理,效率高
r=requests.get('http://img.mshishang.com/pics/2016/1209/20161209125708793.jpeg',stream= True)
#w表示写,b表示二进制,+表示原来有的话删除掉
while open('shuaige2.jpg','wb+') as f:
for chunk inr.iter_content(1024):
f.write(chunk)
4. 提交表单and cookies and 重定向
#提交表单
form={'username':'user','password':'pass'}
r=requests.get('http://httpbin.org/post',data=form)
print(r.text)
r=requests.post('http://httpbin.org/post',data=json.dumps)
print(r.text)
#cookie
url='http://www.baidu.com'
r=requests.get(url)
cookies=r.cookies
for k, v in cookies.get_dict().items():
print(k,v)
5. Beautiful soup
soup=Beautifulsoup(open(url))
相关文章推荐
- python爬虫基础知识——requests、bs4的使用
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
- python3.5——爬虫实现批量下载图片(正则表达式法和BeautifulSoup第三方库法)
- Python下基于requests及BeautifulSoup构建网络爬虫
- Python基础学习——正则表达式与第一个爬虫(requests)
- 网络爬虫及面试中必须掌握的python基础知识(二)
- Python 爬虫实战(一):使用 requests 和 BeautifulSoup
- Python爬虫基础知识及前期准备
- Python爬虫----基础知识(简单爬虫架构、URL管理器和实现方法)
- Python爬虫----基础知识(简单爬虫架构、URL管理器和实现方法)
- Python基础学习-爬虫入门知识
- Python基础知识——urllib模块在爬虫中的应用
- Python配合BeautifulSoup读取网络图片并保存在本地
- python基础===【爬虫】爬虫糗事百科首页图片代码
- Java基础知识IO流(文本文件读取方式二)
- Python 爬虫(1)基础知识和简单爬虫
- python爬虫基础知识
- python requests模块手动设置cookies的几种方式
- caffe的python接口以opencv的方式读取视频,图片做分类时出错的解决
- Python爬虫----爬虫入门(5)---Requests基础