Python 网络爬虫与信息获取(一)—— requests 库的网络爬虫
1. 安装与测试
进入 cmd(以管理员权限),使用 pip 工具,pip install requests 进行安装;
基本用法:
>> import requests
>> r = requests.get('http://www.baidu.com')
# 访问百度主页
>> r.status_code
200
# 状态码,200 表示访问成功
>> r.encoding = 'utf-8'
# 修改编码
>> r.text
# 打印网页内容
2. requests 库的七个主要方法
- request:构造一个请求,是构造以下各方法的基础方法;
- 后续的 6 个方法均需调用 request 方法;
- get:获取 html 网页的主要方法,对应于 http 的 get;
- r = requests.get(url)
- 构造一个向服务器请求资源的 Request 对象;
- 返回一个包含服务器资源的 Response 对象;
- r = requests.get(url)
- head:获取 html 网页头信息,对应于 http 的 head;
- post:向 html 网页提交 post 请求,对应于 http 的 post;
- put:向 html 网页提交 put 请求,对应于 http 的 put;
- patch:向 html 网页提交局部修改请求(patch,补丁,也就是修改,局部更新),对应于 http 的 patch;
- delete:向 html 页面提交删除请求,对应于 http 的 delete;
4. Response 对象的属性
- r.status_code
- r.status_code == requests.codes.ok,如果返回 True,则表示打开正常;
- r.text:http 相应内容的字符串形式,
- r.content:http 相应内容的二进制形式;
- r.encoding:猜测的编码,从 headers 中的 charset 中获得,但并非所有的服务器都会对其相关资源的编码进行规定和要求;
- 如果 headers 中不存在 charset,则认为(猜测)其编码为
ISO-8859-1
- 如果 headers 中不存在 charset,则认为(猜测)其编码为
- r.apparent_encoding:根据内容分析出的编码方式,备选编码;
>> r = requests.get('http://www.baidu.com')
>> r.encoding
'ISO-8859-1'
>> r.apparent_encoding
'utf-8'
>> r.encoding = r.apparent_encoding
5. 与其他库的结合
- BeautifulSoup:做 html 页面的解析;
>> from bs4 import BeautifulSoup
>> r = requests.get(url)
>> BeautifulSoup(r.text).get_text()
Python 网络爬虫与信息获取(一)—— requests 库的网络爬虫的更多相关文章
- 爬虫1.1-基础知识+requests库
目录 爬虫-基础知识+requests库 1. 状态返回码 2. URL各个字段解释 2. requests库 3. requests库爬虫的基本流程 爬虫-基础知识+requests库 关于html ...
- Python爬虫:HTTP协议、Requests库(爬虫学习第一天)
HTTP协议: HTTP(Hypertext Transfer Protocol):即超文本传输协议.URL是通过HTTP协议存取资源的Internet路径,一个URL对应一个数据资源. HTTP协议 ...
- Python爬虫(二):Requests库
所谓爬虫就是模拟客户端发送网络请求,获取网络响应,并按照一定的规则解析获取的数据并保存的程序.要说 Python 的爬虫必然绕不过 Requests 库. 1 简介 对于 Requests 库,官方文 ...
- python之爬虫(四)之 Requests库的基本使用
什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现,其 ...
- Python爬虫学习==>第八章:Requests库详解
学习目的: request库比urllib库使用更加简洁,且更方便. 正式步骤 Step1:什么是requests requests是用Python语言编写,基于urllib,采用Apache2 Li ...
- 整理UWP中网络和设备信息获取的帮助类,需要的拿走。
网络(运营商信息,网络类型) public static class NetworkInfo { /// <summary> /// 网络是否可用 /// </summary> ...
- Python爬虫:HTTP协议、Requests库
HTTP协议: HTTP(Hypertext Transfer Protocol):即超文本传输协议.URL是通过HTTP协议存取资源的Internet路径,一个URL对应一个数据资源. HTTP协议 ...
- python爬虫---从零开始(三)Requests库
1,什么是Requests库 Requests是用python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库. 它比urllib更加方便,可以节约我们大量的工作 ...
- 爬虫入门【2】Requests库简介
发送请求 使用Requests发送网络请求很简单 #首先要导入requests库 import requests #返回一个Response对象 r=requests.get('https://git ...
随机推荐
- 21.Spring Boot 使用Java代码创建Bean并注册到Spring中
转自:https://blog.csdn.net/catoop/article/details/50558333
- Binary Search Algorithm
二分查找代码: //============================================================================ // Name : Bin ...
- 【习题 6-7 UVA - 804】Petri Net Simulation
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟就好 [代码] /* 1.Shoud it use long long ? 2.Have you ever test sever ...
- 【2017 Multi-University Training Contest - Team 10】Schedule
[链接]http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1010&cid=767 [题意] 给一些区间,每台机器在这些区间 ...
- Java中接口继承泛型接口
在使用Mybatis做web开发时,每一个模块的数据持久层的接口都会定义:增删改查四个方法.我想为什么不新建一个Base接口来做所有数据持久层的父接口呢? 于是,我试验了一下,建立了一个泛型接口,里面 ...
- 【算法导论-36】并查集(Disjoint Set)具体解释
WiKi Disjoint是"不相交"的意思.Disjoint Set高效地支持集合的合并(Union)和集合内元素的查找(Find)两种操作,所以Disjoint Set中文翻译 ...
- 2017.1-TOP5 Android开源库
Colorful (Github) Colorful简单实用,通过这个开源库可以通过编码的方式来改变应用的主题,不再需要定义不同的style dependencies { compile 'com.g ...
- (转)kvm虚拟机中,如何给子系统更换光盘
转自:http://www.cnblogs.com/york-hust/archive/2012/06/12/2546334.html 启动kvm后,在kvm窗口中,按下CTRL+ALT+2,切换至q ...
- java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: student is not mapped
Spring 5.0 +Jpa,使用@Query实现 自定义查询报错: java.lang.IllegalArgumentException: org.hibernate.hql.internal.a ...
- IE block my cookie in iframe
---恢复内容开始--- There is a severe bug that a leader figured it out in a published project. In IE11, the ...