(爬虫)requests库
一、requests库简介
urllib库和request库的作用一样,都是服务器发起请求数据,但是requests库比urllib库用起来更方便,它的接口更简单,选用哪种库看自己。
如果没有安装过这个库,需要先 pip install requests 安装。
二、requests库的基本用法
1、发送GET请求
通过get方法去请求百度页面:
import requests
resp = requests.get('http://www.baidu.com/')
# text是requests库以自己猜测的解码方式去解码,所以可能会出现乱码的问题
print(resp.text)
# content返回的bytes的数据,所以可以根据自己指定的解码方式去解码就不会出现问题
print(resp.content.decode('utf-8'))
# url获取当前请求的url
print(resp.url)
# encoding获取当前页面的编码方式
print(resp.encoding)
# status_code获取当前的状态码
print(resp.status_code)
那么如何添加请求参数和headers请求头信息呢,requests库现在就比urllib库好用简单了,urllib库还需要手动对请求的参数进行编码才能去请求,而requests库则将这个步骤在底层进行封装了,只需要将参数传递进去即可,如下:
import requests url = 'http://www.baidu.com/s'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}
params = {
'wd': '林俊杰'
}
resp = requests.get(url, params=params, headers=headers)
print(resp.url) with open('baidu.html', 'w', encoding='utf-8') as f:
f.write(resp.content.decode('utf-8'))
2、发送POST请求
我们以拉钩网为例,去爬取拉勾网的职位信息:
import requests url = 'https://www.lagou.com/jobs/positionAjax.json?city=%E5%8C%97%E4%BA%AC&needAddtionalResult=false'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
}
data = {
'first': 'true',
'pn': 1,
'kd': 'python'
} resp = requests.post(url, data=data, headers=headers)
print(resp.json())
3、使用代理
requests库使用代理就非常简单了,只需要在请求的方法(get、post)中添加proxies参数即可:
import requests
proxy = {
'http': '115.218.216.251:9000'
}
resp = requests.get(url='http://www.httpbin.org/ip', proxies=proxy)
print(resp.text)
4、cookie模拟登陆
我们可以通过cookie获取到cookie信息:
import requests
resp = requests.get('http://www.baidu.com/')
# cookie 返回cookie对象
print(resp.cookies)
# get_dict 将cookie信息以字典的形式返回
print(resp.cookies.get_dict())
urllib库可以使用opener发送多个请求,并且多个请求之间是共享cookie的,requests库也要达到共享cookie的目的,我们可以使用requests库提供的session对象,这里的session不是web中的那个session,这里只是一个会话的对象而已,下面以登录人人网为例,来使用cookie来登录:
import requests login_url = 'http://www.renren.com/PLogin.do'
dapeng_url = 'http://www.renren.com/880151247/profile'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
}
data = {
'email': '',
'password': 'xxxxx'
} session = requests.Session() session.post(url=login_url, data=data, headers=headers) resp = session.get(url=dapeng_url, headers=headers)
with open('renren.html', 'w', encoding='utf-8') as f:
f.write(resp.content.decode('utf-8'))
(爬虫)requests库的更多相关文章
- Python爬虫—requests库get和post方法使用
目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...
- Python爬虫--Requests库
Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,requests是python实现的最简单易用的HTTP库, ...
- 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】
[写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...
- [爬虫] requests库
requests库的7个常用方法 requests.request() 构造一个请求,支撑以下各种方法的基础方法 requests.get() 获取HTML网页的主要方法,对应于HTTP的GET re ...
- Python爬虫 requests库基础
requests库简介 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支 ...
- python爬虫---requests库的用法
requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下 ...
- Python 爬虫-Requests库入门
2017-07-25 10:38:30 response = requests.get(url, params=None, **kwargs) url : 拟获取页面的url链接∙ params : ...
- Python爬虫---requests库快速上手
一.requests库简介 requests是Python的一个HTTP相关的库 requests安装: pip install requests 二.GET请求 import requests # ...
- 4.爬虫 requests库讲解 GET请求 POST请求 响应
requests库相比于urllib库更好用!!! 0.各种请求方式 import requests requests.post('http://httpbin.org/post') requests ...
- 6.爬虫 requests库讲解 总结
requests库的总结: 用ProcessOn根据前面的几节内容做了个思维导图:
随机推荐
- C#净化版WebApi框架
前言 我们都知道WebApi是依赖于Asp.Net MVC的HttpRouteCollection进行路由 . 但WebApi和MVC之间是没有依赖关系的, WebApi的基类ApiControlle ...
- Spring中的@conditional注解
今天主要从以下几方面来介绍一下@Conditional注解 @Conditional注解是什么 @Conditional注解怎么使用 1,@Conditional注解是什么 @Conditional注 ...
- v4v7升级到androidx过程
因为原项目应用的都是v4v7包,谷歌改成androidx后就升级了一番,首先在properties文件 然后在菜单里点击升级,studio会帮你把报名什么的都改掉 打开项目,发现都自动改掉了,完美,然 ...
- 【English】20190428
It is of paramount importance that极为重要的一点[pærəmaʊnt] strategizing around SOA围绕soa制定战略 efficient gov ...
- MongoDB 文章目录
基础: MongoDB入门系列(一):基础概念和安装 MongoDB入门系列(二):Insert.Update.Delete.Drop MongoDB入门系列(三):查询(SELECT) MongoD ...
- Java小白如何一步步学好Java,听听企业Java培训师的实践经验吧
今天我准备给小主展示一篇Java培训老师的文章,希望能给Java小白一个学好Java的路径或者提示.以下就是原文: 从大学到现在,我使用Java已经将近20年,日常也带实习生,还在公司内部做train ...
- Flarum轻量级论坛的安装
论坛作为互联网中的远古产物,经历了如QQ群.社区和贴吧等新兴社交工具的冲击,依然能够存在,肯定是有着不可替代的用处,像吾爱.远景等论坛依旧火热.一些博客主也喜欢自己搭建一个论坛作为用户聚集之地. 之前 ...
- C#在window服务配置Log4Net.dll
1.使用背景: C#window服务下添加一个日志记录程序集(Log4Net.dll) 2.添加和使用步骤如下: 一.下载并引入Log4Net.dll程序集到项目中 下载地址:http://loggi ...
- Hangfire源码解析-任务是如何执行的?
一.Hangfire任务执行的流程 任务创建时: 将任务转换为Type并存储(如:HangFireWebTest.TestTask, HangFireWebTest, Version=1.0.0.0, ...
- Elasticsearch Index模块
1. Index Setting(索引设置) 每个索引都可以设置索引级别.可选值有: static :只能在索引创建的时候,或者在一个关闭的索引上设置 dynamic:可以动态设置 1.1. S ...