requests库比urllib库更加方便,包含了很多功能。

1、在使用之前需要先安装pip,在pycharm中打开:

写入pip install requests命令,即可下载

在github中有关于requests库的介绍,网址:https://github.com/requests/requests

2、Get请求

response=requests.get("https://www.baidu.com/")

我们要完成在百度的页面获取中国的相关信息,相当于

输入中国:

用爬虫代码实验实现:

import requests

# wd是在网址中后面的一段
params={
'wd':'中国'
} headers={
'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
}
# 这时我们要在这个网址中加入S
response=requests.get("http://www.baidu.com/s",params=params,headers=headers) with open('baidu.html','w',encoding='utf-8') as fp:
fp.write(response.content.decode('utf-8'))

打开后就是中国的相关信息:

3、response.txt和response.content的区别

response.txt是 requests是经response.content解码的字符串,requests会根据自己的猜测来进行解码,有时候会猜测错误,导致乱码。

response.content是直接从网上爬取的数据,没有经过经过任何解码,是bytes类型。

所以最常用的就是:response.content.decode('utf-8')

python爬虫(八) requests库之 get请求的更多相关文章

  1. Python爬虫之requests库介绍(一)

    一:Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 ...

  2. Python爬虫:requests 库详解,cookie操作与实战

    原文 第三方库 requests是基于urllib编写的.比urllib库强大,非常适合爬虫的编写. 安装: pip install requests 简单的爬百度首页的例子: response.te ...

  3. python爬虫之requests库

    在python爬虫中,要想获取url的原网页,就要用到众所周知的强大好用的requests库,在2018年python文档年度总结中,requests库使用率排行第一,接下来就开始简单的使用reque ...

  4. Python爬虫之requests库的使用

    requests库 虽然Python的标准库中 urllib模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests宣传是 "HTTP for ...

  5. 【Python爬虫】Requests库的基本使用

    Requests库的基本使用 阅读目录 基本的GET请求 带参数的GET请求 解析Json 获取二进制数据 添加headers 基本的POST请求 response属性 文件上传 获取cookie 会 ...

  6. python爬虫(1)requests库

    在pycharm中安装requests库的一种方法 首先找到设置 搜索然后安装,蓝色代表已经安装 requests库中的get请求 与HTTP协议相对应,requests库也有七种请求方式. 获取ur ...

  7. python爬虫之requests库介绍(二)

    一.requests基于cookie操作 引言:有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们 ...

  8. Python爬虫之Requests库的基本使用

    import requests response = requests.get('http://www.baidu.com/') print(type(response)) print(respons ...

  9. Python爬虫系列-Requests库详解

    Requests基于urllib,比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求. 实例引入 import requests response = requests.get( ...

  10. python 爬虫 基于requests模块的get请求

    需求:爬取搜狗首页的页面数据 import requests # 1.指定url url = 'https://www.sogou.com/' # 2.发起get请求:get方法会返回请求成功的响应对 ...

随机推荐

  1. linux php 扩展安装

    phpize./configure --with-php-config=/usr/bin/php-config make && make install

  2. c++ google glog模块安装和基本使用(ubuntu)环境

    1,如何安装 1 Git clone https://github.com/google/glog.git 2 cd glog 3 ./autogen.sh 4 ./configure --prefi ...

  3. 【网易官方】极客战记(codecombat)攻略-地牢-橱柜里的骷髅

    关卡连接: https://codecombat.163.com/play/level/cupboards-of-kithgard 谁知道什么样的恐怖事情潜伏在 Kithgard 的橱柜里? 简介: ...

  4. 1.1 Eclipse下载安装(+java环境)

    可直接上官网下载:http://www.eclipse.org/downloads/ 直接下载地址:http://www.eclipse.org/downloads/download.php?file ...

  5. ACM-ICPC实验室20.2.22测试-动态规划

    C.田忌赛马 直接贪心做就可以~ #include<bits/stdc++.h> using namespace std; ; int a[maxn],b[maxn]; int main( ...

  6. Ubuntu中获取和使用uiautomatorviewer

    图省事的办法是直接在网上找个android platform tools的某个版本下载下来 比如这个网站里面的:Android SDK工具——Platform-tools 个人比较倾向于先下载andr ...

  7. app内区域截图利用html2Canvals保存到手机 截屏 (html2Canvas使用版本是:0.5.0-beta3。)

    app内区域截图利用html2Canvals保存到手机 app内有时候需要区域内的截图保存dom为图像,我们可以使用html2Canvas将dom转换成base64图像字符串,然后再利用5+api保存 ...

  8. Hadoop3.1.1源码Client详解 : 入队前数据写入

    该系列总览: Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览 紧接着上一篇: Hadoop3.1.1源码Client详解 : 写入准备-RPC调用与流的建立 先给出 ...

  9. SQL常用语句和函数

    从一个表中选取数据插入到另一个表中: select column_name(s) into new_table_name from old_table_name --new_table_name表不必 ...

  10. 如何修改mysql 默认引擎为InnoDB?

    1.查看 MySQL支持的引擎有哪些? show engines: 结果: 由上图可以看出,只有 InnoDB 是支持事务的 2.查看默认引擎 show variables like “default ...