一. 登录事例

a. 查找汽车之家新闻 标题 链接 图片写入本地

import requests
from bs4 import BeautifulSoup
import uuid response = requests.get(
'http://www.autohome.com.cn/news/'
)
response.encoding = 'gbk'
soup = BeautifulSoup(response.text,'html.parser') # HTML会转换成对象
tag = soup.find(id='auto-channel-lazyload-article')
li_list = tag.find_all('li') for i in li_list:
a = i.find('a')
if a:
print(a.attrs.get('href'))
txt = a.find('h3').text
print(txt)
img_url = txt = a.find('img').attrs.get('src')
print(img_url) img_response = requests.get(url=img_url)
file_name = str(uuid.uuid4()) + '.jpg'
with open(file_name,'wb') as f:
f.write(img_response.content)

用到BeautifulSoup模块寻找标签

b. 抽屉点赞 获取页面和登录都会获取gpsd  点赞会使用获取页面的gpsd 而不是登录的gpsd

import requests

#先获取页面

r1 = requests.get('http://dig.chouti.com/')
r1_cookies = r1.cookies.get_dict() #登录
post_dict = {
"phone":"",
"password":"woshiniba",
"oneMonth":""
} r2 = requests.post(
url="http://dig.chouti.com/login",
data = post_dict,
cookies=r1_cookies
) r2_cookies = r2.cookies.get_dict() # 访问其他页面
r3 = requests.post(
url="http://dig.chouti.com/link/vote?linksId=13921091",
cookies={'gpsd':r1_cookies['gpsd']}
)
print(r3.text)

抽屉网页面的(gpsd)

c. 登录githup 携带cookie登录

import requests
from bs4 import BeautifulSoup r1 = requests.get('https://github.com/login')
s1 = BeautifulSoup(r1.text,'html.parser') # 获取csrf_token
token = s1.find(name='input',attrs={'name':"authenticity_token"}).get('value')
r1_cookie_dict = r1.cookies.get_dict() # 将用户名 密码 token 发送到服务端 post
r2 = requests.post(
'https://github.com/session',
data={
'commit':'Sign in',
'utf8':'✓',
'authenticity_token':token,
'login':'317828332@qq.com',
'password':'alex3714'
},
cookies=r1_cookie_dict
) # 获取登录后cookie
r2_cookie_dict = r2.cookies.get_dict() #合并登录前的cookie和登录后的cookie
cookie_dict = {}
cookie_dict.update(r1_cookie_dict)
cookie_dict.update(r2_cookie_dict) r3 = requests.get(
url='https://github.com/settings/emails',
cookies=cookie_dict
) print(r3.text)

二.  requests 参数

- method:  提交方式
- url: 提交地址
- params: 在URL中传递的参数,GET
- data: 在请求体里传递的数据
- json 在请求体里传递的数据
- headers 请求头
- cookies Cookies
- files 上传文件
- auth 基本认知(headers中加入加密的用户名和密码)
- timeout 请求和响应的超市时间
- allow_redirects 是否允许重定向
- proxies 代理
- verify 是否忽略证书
- cert 证书文件
- stream 村长下大片
- session: 用于保存客户端历史访问信息

a. file 发送文件

import requests

requests.post(
url='xxx',
filter={
'name1': open('a.txt','rb'), #名称对应的文件对象
'name2': ('bbb.txt',open('b.txt','rb')) #表示上传到服务端的名称为 bbb.txt
}
)

b. auth 认证

#配置路由器访问192.168.0.1会弹出小弹窗,输入用户名,密码 点击登录不是form表单提交,是基本登录框,这种框会把输入的用户名和密码 经过加密放在请求头发送过去
import requests

requests.post(
url='xxx',
filter={
'name1': open('a.txt','rb'), #名称对应的文件对象
'name2': ('bbb.txt',open('b.txt','rb')) #表示上传到服务端的名称为 bbb.txt
}
)

c. stream 流

#如果服务器文件过大,循环下载

def param_stream():
ret = requests.get('http://127.0.0.1:8000/test/', stream=True)
print(ret.content)
ret.close() # from contextlib import closing
# with closing(requests.get('http://httpbin.org/get', stream=True)) as r:
# # 在此处理响应。
# for i in r.iter_content():
# print(i)

d. session  和django不同  事例:简化抽屉点赞

    import requests

    session = requests.Session()

    ### 、首先登陆任何页面,获取cookie

    i1 = session.get(url="http://dig.chouti.com/help/service")

    ### 、用户登陆,携带上一次的cookie,后台对cookie中的 gpsd 进行授权
i2 = session.post(
url="http://dig.chouti.com/login",
data={
'phone': "",
'password': "xxxxxx",
'oneMonth': ""
}
) i3 = session.post(
url="http://dig.chouti.com/link/vote?linksId=8589623",
)
print(i3.text)

python爬虫之requests模块的更多相关文章

  1. 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块

    孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...

  2. Python爬虫练习(requests模块)

    Python爬虫练习(requests模块) 关注公众号"轻松学编程"了解更多. 一.使用正则表达式解析页面和提取数据 1.爬取动态数据(js格式) 爬取http://fund.e ...

  3. Python 爬虫二 requests模块

    requests模块 Requests模块 get方法请求 整体演示一下: import requests response = requests.get("https://www.baid ...

  4. Python爬虫之requests模块(1)

    一.引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃 ...

  5. Python爬虫之requests模块(2)

    一.今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 二.回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 三. ...

  6. python爬虫值requests模块

    - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在 ...

  7. Python爬虫(requests模块)

     Requests是唯一的一个非转基因的Python HTTP库,人类可以安全享用. Requests基础学习 使用方法: 1.导入Requests模块: import requests 2.尝试用g ...

  8. 【python爬虫】requests模块

    文档:从 pythoneer 到 pythonista 的100个模块 链接:http://note.youdao.com/noteshare?id=2b95bb3651c21af80ca1936f8 ...

  9. python爬虫之requests模块介绍

    介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requests库发送请求将网页内容下 ...

  10. python 爬虫 基于requests模块发起ajax的post请求

    基于requests模块发起ajax的post请求 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定某个城市地点的餐厅数据 点击肯德基餐厅查 ...

随机推荐

  1. Hadoop2.0环境搭建

    需准备的前提条件: 1. 安装JDK(自行安装) 2. 关闭防火墙(centos): systemctl stop firewalld.service systemctl disable firewa ...

  2. mybatis-3 Dynamic SQL

    Dynamic SQL One of the most powerful features of MyBatis has always been its Dynamic SQL capabilitie ...

  3. laravel 图片上传 ajax 方式

    laravel 图片上传 //后台轮播图上传 $("#img-upload").on('submit',function(e){ e.preventDefault(); var f ...

  4. 一只代码小白git托管路上的二三事

    [经验]一只代码小白git托管路上的二三事 写在前面的话 寒假的时候,娄老师给我们布置了代码托管的作业,并要求把托管地址发给学委.因假期的时候没有带电脑回家,所以只是在手机上草草注册了,也稀里糊涂就将 ...

  5. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  6. java中数组以及集合

    java中数组: 数组在Java里是一种特殊类型,有别于普通的“类的实例”的对象.但实际数组也是一种对象类型,int[]a = new int[5]  a是在java栈中分配的引用变量,类型是int[ ...

  7. 枚举转SelectList扩展方法

        public enum Avbc    {        Red=1,        Blue=2,        Whilt=3,        Black=4    } public st ...

  8. 20145216史婧瑶《Java程序设计》第5周学习总结

    20145216 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承架构 Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生 ...

  9. 20145302张薇《Java程序设计》实验二报告

    20145302张薇<Java程序设计>实验二:Java面向对象程序设计 使用TDD的方式设计实现复数类:Complex 测试代码 import org.junit.Test; publi ...

  10. 20145329《Java程序设计》第六周学习总结

    教材学习内容总结 第十章 InputSream与OutputStream Java中,输入串流代表对象为java.io.InputStream实例,输出串流代表对象为java.io.OutputStr ...