美文网首页
Python使用Requests请求网页

Python使用Requests请求网页

作者: __豆约翰__ | 来源:发表于2018-12-19 07:08 被阅读97次

安装方式

利用 pip 安装

$ pip install requests

基本GET请求(headers参数 和 parmas参数)

1. 最基本的GET请求可以直接用get方法

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

# 也可以这么写
# response = requests.request("get", "http://www.baidu.com/")

2. 添加 headers 和 查询参数

如果想添加 headers,可以传入headers参数来增加请求头中的headers信息。如果要将参数放在url中传递,可以利用 params 参数。


import requests

kw = {'wd':'长城'}

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}

# params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()
response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers)

# 查看响应内容,response.text 返回的是Unicode格式的数据
print (response.text)

# 查看响应内容,response.content返回的字节流数据
print (respones.content)

# 查看完整url地址
print (response.url)

# 查看响应头部字符编码
print (response.encoding)

# 查看响应码
print (response.status_code)

运行结果

......

......

'http://www.baidu.com/s?wd=%E9%95%BF%E5%9F%8E'

'utf-8'

200

  • 使用response.text 时,Requests 会基于 HTTP 响应的文本编码自动解码响应内容,大多数 Unicode 字符集都能被无缝地解码。
  • 使用response.content 时,返回的是服务器响应数据的原始二进制字节流,可以用来保存图片等二进制文件。

小栗子

通过requests获取新浪首页


import  requests
response = requests.get("http://www.sina.com.cn")
print(response.request.headers)
print(response.content.decode())

结果

{'User-Agent': 'python-requests/2.12.4', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
<!DOCTYPE html>
<!-- [ published at 2017-06-09 15:15:23 ] -->
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>新浪首页</title>
    <meta name="keywords" content="新浪,新浪网,SINA,sina,sina.com.cn,新浪首页,门户,资讯" />
  ...


import  requests
response = requests.get("http://www.sina.com.cn")
print(response.request.headers)
print(response.text)

结果

{'User-Agent': 'python-requests/2.12.4', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
<!DOCTYPE html>
<!-- [ published at 2017-06-09 15:18:10 ] -->
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>新浪首页</title>
    <meta name="keywords" content="新浪,新浪网,SINA,sina,sina.com.cn,新浪首页,门户,资讯" />
    <meta name="description" content="新浪网为全�用户24�时�供全��时的中文资讯,内容覆盖国内外��新闻事件�体�赛事�娱�时尚�产业资讯�实用信�等,设有新闻�体育�娱��财��科技�房产�汽车等30多个内容频�,�时开设�客�视频�论�等自由互动交�空间。" />
    <link rel="mask-icon" sizes="any" href="//www.sina.com.cn/favicon.svg" color="red">
`

产生问题的原因分析
  1. requests默认自带的Accept-Encoding导致或者新浪默认发送的就是压缩之后的网页
  2. 但是为什么content.read()没有问题,因为requests,自带解压压缩网页的功能
  3. 当收到一个响应时,Requests 会猜测响应的编码方式,用于在你调用response.text 方法时对响应进行解码。Requests 首先在 HTTP 头部检测是否存在指定的编码方式,如果不存在,则会使用 chardet.detect来尝试猜测编码方式(存在误差)
  4. 更推荐使用response.content.deocde()

通过requests获取网络上的图片

import requests
img_url = "http://imglf1.ph.126.net/pWRxzh6FRrG2qVL3JBvrDg==/6630172763234505196.png"
response = requests.get(img_url)
with open('baidu_tieba.jpg', 'ab') as f:
    f.write(response.content)
    f.close()

相关文章

  • Requests库基本使用

    requests是python实现的最简单易用的HTTP库,建议爬虫使用requests 获取某个网页 各种请求 ...

  • Python使用Requests请求网页

    安装方式 利用 pip 安装 基本GET请求(headers参数 和 parmas参数) 1. 最基本的GET请求...

  • Requests 使用笔记

    官网:http://docs.python-requests.org 使用会话打开网页 会话对象能够跨请求保持某些...

  • 使用Requests发送POST请求

    接口: Python 使用requests发送POST请求: 运行结果: 学习网址:Requests中文文档

  • 20.Python使用Requests请求网页

    安装方式 利用 pip 安装 基本GET请求(headers参数 和 parmas参数) 最基本的GET请求可以直...

  • 爬虫进阶《requests 模块学习》

    使用 Requests 发送网络请求非常简单。 一开始要导入 Requests 模块: 然后,尝试获取某个网页 现...

  • python3爬取12306余票,自动抢票

    环境:python3.6 编辑器:pycharm 用到的模块:requests(网页请求)json(把数据转字典)...

  • python3 requests详解

    requests库是一个常用的用于http请求的模块,它使用python语言编写,可以方便的对网页进行爬取,是学习...

  • python接口测试

    一、requests库二、pandas库三、接口示例 前言本文档讲述使用python requests库请求接口、...

  • python 网页爬虫

    centos 7下python2.7实现网页爬虫使用requests、BeautifulSoup 安装 方法一: ...

网友评论

      本文标题:Python使用Requests请求网页

      本文链接:https://www.haomeiwen.com/subject/mddrkqtx.html