一. 登录事例

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. apache服务器设置

    服务器目录 目录说明 bin: apache常用的一些命令 cgi-bin:存放一些脚本语言命令 conf:apache配置文件 error:错误记录 htodcs存放站点文件 logs:记录日志 m ...

  2. java线程方面的知识

    java中单继承,多实现的: 若为多继承,那么当多个父类中有重复的属性或者方法时,子类的调用结果会含糊不清,因此用了单继承. 为什么是多实现呢? 通过实现接口拓展了类的功能,若实现的多个接口中有重复的 ...

  3. 深入探讨JS中的数组排序函数sort()和reverse()

    最近在研究Javascript发现了其中一些比较灵异的事情.有点让人感到无语比如: alert(typeof( NaN == NaN));//结果为假. alert(typeof( NaN != Na ...

  4. Swoole学习(三)Swoole之UDP服务器的创建

    环境:Centos6.4,PHP环境:PHP7 <?php //创建UCP服务器(UDP服务器相对于TCP服务器通信更可靠些) /** * $host 是swoole需要监听的ip,如果要监听本 ...

  5. vmware基于主机模式实现上网(win10)

    首先查看本机win10的网络情况: 网卡VMnet1就是主机模式的网卡,确认本机win10共享了网络给vmnet1这张网卡,如果没有共享,那么进行设置: 进行上述设置,然后在vmnet1网卡上设置ip ...

  6. win10 安装 tensorflow 并运行helloworld

    win10 安装 tensorflow 并运行helloworld   折腾了一下,在win10上成功安装tensorflow. 1 下载安装python,注意一定要是64位(比如python-3.5 ...

  7. RocEDU.阅读.写作《苏菲的世界》书摘

    我们在成长的过程当中,似乎失去了对这世界的好奇心.也正因此,我们丧失了某种极为重要的能力(这也是一种哲学家们想要使人们恢复的能力).因为,在我们内心的某处,有某个声音告诉我们:生命是一种很庞大的.神秘 ...

  8. 20145328 《Java程序设计》课程总结

    20145328 <Java程序设计>课程总结 每周学习总结链接汇总 20145328 <Java程序设计>第0周学习总结 20145328 <Java程序设计>第 ...

  9. 【前端】jQuery选择器$()的实现原理

    今天三七互娱技术面试的时候面试官问了我这个问题,当时一脸懵逼,于是好好总结一下. 当我们使用jquery选择器的时候,$(s).回默认去执行jquery内部封装好的一个init的构造函数每次申明一个j ...

  10. Java数字证书操作

    为服务器生成证书 keytool -genkey -v -alias tomcat -keyalg RSA -keystore D:\tomcat.keystore -validity 36500 为 ...