python 爬虫 Requests简单使用

python 爬虫 Requests简单使用

Requests的文档非常完备,中文文档也相当不错。Requests能完全满足当前网络的需求,支持Python 2.6–3.5,而且能在PyPy下完美运行。

开源地址:https://github.com/kennethreitz/requests

中文文档 API: http://docs.python-requests.org/zh_CN/latest/index.html

安装方式

利用 pip3 安装 或者利用 easy_install 都可以完成安装:

pip3 install requests
easy_install requests

基本GET请求(headers参数 和 parmas参数)

import requests
kw={'q':'长城'}
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
print(type(headers))
print(type(kw))
r = requests.get('https://www.so.com/s?',params=kw,headers=headers)
print(r.text)
print(r.content)
print(r.url)
print(r.encoding)
print(r.status_code)

在这里插入图片描述
通过requests获取网络上图片,并用PIL获取图片的大小
PIL (Python Imaging Library)
Python图像处理库,该库支持多种文件格式,提供强大的图像处理功能。python 3.5 以上集成到pillow里需要

pip install pillow
import re
import requests
import io
from PIL import Image

# help(io)
# help(requests)
img_url='https://p1.ssl.qhimg.com/t01bf0d2fc436602caf.jpg'
r = requests.get(img_url)
# print(r)
# help(r)
# print(r.content)
help(Image)

f= io.BytesIO(r.content)

img=Image.open(f)
help(img)
print(type(img))
print(img.height)
print(img.size)
print(img.width)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值