“Python requests库的使用”的版本间的差异
来自CloudWiki
(→requests库的用法) |
小 (恢复58.56.20.141(讨论)的编辑至Cloud17的最后版本) |
||
(未显示2个用户的5个中间版本) | |||
第1行: | 第1行: | ||
==requests库概述== | ==requests库概述== | ||
+ | 中文版概述: http://docs.python-requests.org/zh_CN/latest/ | ||
+ | |||
+ | 英文版概述: http://docs.python-requests.org/en/master/ | ||
+ | |||
==安装resuests库== | ==安装resuests库== | ||
+ | *执行命令: | ||
+ | pip3 install requests | ||
*具体安装方法,可参考[[Python第三方库的安装]] | *具体安装方法,可参考[[Python第三方库的安装]] | ||
+ | |||
==requests库的用法== | ==requests库的用法== | ||
+ | ===获取网页=== | ||
+ | 通过get方法返回网页,返回的是一个对象;网页内容作为一个对象更好操作。 | ||
+ | <nowiki>>>> import requests | ||
+ | >>> r = requests.get("http://www.baidu.com")#打开一个网页 | ||
+ | >>> type(r) | ||
+ | <class 'requests.models.Response'></nowiki> | ||
+ | |||
+ | ===常用属性=== | ||
+ | Response对象常用的属性有: | ||
+ | *status_code:返回请求HTTP后的状态 | ||
+ | *text:请求的页面内容 | ||
+ | *encoding:返回页面内容的编码方式 | ||
+ | *content:页面内容的二进制形式 | ||
+ | |||
*常用用法: | *常用用法: | ||
<nowiki>>>> import requests | <nowiki>>>> import requests | ||
第15行: | 第36行: | ||
>>> print(r.text) | >>> print(r.text) | ||
'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head></nowiki> | '<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head></nowiki> | ||
+ | |||
+ | ===异常处理=== | ||
+ | 初始版代码:将网页直接打印 | ||
+ | <nowiki>import requests | ||
+ | def getHTMLText(url): | ||
+ | try: | ||
+ | r = requests.get(url, timeout=30) | ||
+ | r.raise_for_status() #如果状态不是200,引发异常 | ||
+ | r.encoding = 'utf-8' #无论原来用什么编码,都改成utf-8 | ||
+ | return r.text | ||
+ | except: | ||
+ | return "" | ||
+ | while(True): | ||
+ | url = input("请输入带http头的网址:") | ||
+ | if url =='quit': | ||
+ | break; | ||
+ | print(getHTMLText(url))</nowiki> | ||
+ | |||
+ | 改进版代码:将网页保存为文件 | ||
+ | |||
+ | |||
返回 [[Python网络爬虫和自动化]] | 返回 [[Python网络爬虫和自动化]] |
2018年6月15日 (五) 01:38的最新版本
requests库概述
中文版概述: http://docs.python-requests.org/zh_CN/latest/
英文版概述: http://docs.python-requests.org/en/master/
安装resuests库
- 执行命令:
pip3 install requests
- 具体安装方法,可参考Python第三方库的安装
requests库的用法
获取网页
通过get方法返回网页,返回的是一个对象;网页内容作为一个对象更好操作。
>>> import requests >>> r = requests.get("http://www.baidu.com")#打开一个网页 >>> type(r) <class 'requests.models.Response'>
常用属性
Response对象常用的属性有:
- status_code:返回请求HTTP后的状态
- text:请求的页面内容
- encoding:返回页面内容的编码方式
- content:页面内容的二进制形式
- 常用用法:
>>> import requests >>> r = requests.get("http://www.baidu.com")#打开一个网页 >>> print(r.status_code) #返回状态 200 >>> print(r.text) #观察返回的内容 '<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>ç\x99¾åº¦ä¸\x80ä¸\x8bï¼\x8cä½\xa0å°±ç\x9f¥é\x81\x93</title></head> ... >>>r.encoding #查看编码方式,因为是这种,所以乱码 'ISO-8859-1' >>> r.encoding = 'utf-8' >>> print(r.text) '<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head>
异常处理
初始版代码:将网页直接打印
import requests def getHTMLText(url): try: r = requests.get(url, timeout=30) r.raise_for_status() #如果状态不是200,引发异常 r.encoding = 'utf-8' #无论原来用什么编码,都改成utf-8 return r.text except: return "" while(True): url = input("请输入带http头的网址:") if url =='quit': break; print(getHTMLText(url))
改进版代码:将网页保存为文件