python爬虫之requests模块
一. 登录事例
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模块的更多相关文章
- 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块
孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...
- Python爬虫练习(requests模块)
Python爬虫练习(requests模块) 关注公众号"轻松学编程"了解更多. 一.使用正则表达式解析页面和提取数据 1.爬取动态数据(js格式) 爬取http://fund.e ...
- Python 爬虫二 requests模块
requests模块 Requests模块 get方法请求 整体演示一下: import requests response = requests.get("https://www.baid ...
- Python爬虫之requests模块(1)
一.引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃 ...
- Python爬虫之requests模块(2)
一.今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 二.回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 三. ...
- python爬虫值requests模块
- 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在 ...
- Python爬虫(requests模块)
Requests是唯一的一个非转基因的Python HTTP库,人类可以安全享用. Requests基础学习 使用方法: 1.导入Requests模块: import requests 2.尝试用g ...
- 【python爬虫】requests模块
文档:从 pythoneer 到 pythonista 的100个模块 链接:http://note.youdao.com/noteshare?id=2b95bb3651c21af80ca1936f8 ...
- python爬虫之requests模块介绍
介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requests库发送请求将网页内容下 ...
- python 爬虫 基于requests模块发起ajax的post请求
基于requests模块发起ajax的post请求 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定某个城市地点的餐厅数据 点击肯德基餐厅查 ...
随机推荐
- @RequestMapping @SessionAttributes @ModelAttribute注解用法
简介: @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestM ...
- 在python3下使用OpenCV 抓取摄像头图像提取蓝色
工作中需要对摄像头进行调试, Python平台大大提高调试效率. 从网找到段代码, 可以从摄像头图像中抠出蓝色. import cv2 import numpy as np cap = cv2.Vi ...
- c/c++调用dll
1.lib.h 1 #ifndef LIB_H 2 #define LIB_H 3 4 #include <windows.h> 5 #include <iostream> 6 ...
- 20145303刘俊谦 《Java程序设计》第十周学习总结
教材学习内容总结 网络编程 就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时,大部 ...
- 20145310《Java程序设计》第4次实验报告
20145310<Java程序设计>第4次实验报告 实验内容 搭建Android环境 运行Android 修改代码并输出自己的学号 实验步骤 搭建Android环境 安装Android S ...
- linux及安全第三周总结——20135227黄晓妍
总结部分: Linux内核源代码: Arch 支持不同cpu的源代码:主要关注x86 Init 内核启动的相关代码:主要关注main.c,整个Linux内核启动代码start_kernel函数 K ...
- 33c3-pwn500-recurse
Recurse 好记性不如烂笔头.当时没有记录,现在趁着有时间简单写一写,为以后留备份. 这个题目当时并没有队伍做出来,赛后作者发布了题目的源码和解答.看了之后发现是一个UAF漏洞,不过漏洞很不好找. ...
- CSS控制滚动条的样式
到今天(2018年10月25日)为止, 这还是chrome上的一个实验性特性: ::-webkit-scrollbar{width:4px;height:4px;} ::-webkit-scrollb ...
- vue.js的一些事件绑定和表单数据双向绑定
知识点: v-on:相当于: 例如:v-on:click==@click ,menthods事件绑定 v-on修饰符可以指定键盘事件 v-model进行表单数据的双向绑定 <template&g ...
- Sublime Text指南
转自: http://lucida.me/blog/sublime-text-complete-guide/ 摘要(Abstract) 本文系统全面的介绍了Sublime Text,旨在成为最优秀的 ...