需求:爬取搜狗首页的页面数据

import requests

# 1.指定url
url = 'https://www.sogou.com/' # 2.发起get请求:get方法会返回请求成功的响应对象
response = requests.get(url=url) # 3.获取响应中的数据:text属性作用是可以获取响应对象中字符串形式的页面数据
page_data = response.text # 4.持久化数据
with open("sougou.html","w",encoding="utf-8") as f:
f.write(page_data)
f.close()
print("ok")

requests模块如何处理携带参数的get请求,返回携带参数的请求

需求:指定一个词条,获取搜狗搜索结果所对应的页面数据

之前urllib模块处理url上参数有中文的需要处理编码,requests会自动处理url编码

发起带参数的get请求

params可以是传字典或者列表

def get(url, params=None, **kwargs):
r"""Sends a GET request. :param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
import requests

# 指定url
url = 'https://www.sogou.com/web'
# 封装get请求参数
prams = {
'query':'周杰伦',
'ie':'utf-8'
} response = requests.get(url=url,params=prams)
page_text = response.text
with open("周杰伦.html","w",encoding="utf-8") as f:
f.write(page_text)
f.close()
print("ok")

利用requests模块自定义请求头信息,并且发起带参数的get请求

get方法有个headers参数 把请求头信息的字典赋给headers参数

import requests

# 指定url
url = 'https://www.sogou.com/web'
# 封装get请求参数
prams = {
'query':'周杰伦',
'ie':'utf-8'
} # 自定义请求头信息
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
} response = requests.get(url=url,params=prams,headers=headers)
page_text = response.text
with open("周杰伦.html","w",encoding="utf-8") as f:
f.write(page_text)
f.close()
print("ok")

python 爬虫 基于requests模块的get请求的更多相关文章

  1. python 爬虫 基于requests模块发起ajax的post请求

    基于requests模块发起ajax的post请求 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定某个城市地点的餐厅数据 点击肯德基餐厅查 ...

  2. python 爬虫 基于requests模块发起ajax的get请求

    基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面的请求 鼠标往下 ...

  3. Python爬虫之requests模块(1)

    一.引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃 ...

  4. python爬虫值requests模块

    - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在 ...

  5. Python爬虫练习(requests模块)

    Python爬虫练习(requests模块) 关注公众号"轻松学编程"了解更多. 一.使用正则表达式解析页面和提取数据 1.爬取动态数据(js格式) 爬取http://fund.e ...

  6. 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块

    孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...

  7. Python 爬虫二 requests模块

    requests模块 Requests模块 get方法请求 整体演示一下: import requests response = requests.get("https://www.baid ...

  8. Python爬虫之requests模块(2)

    一.今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 二.回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 三. ...

  9. Python接口测试-使用requests模块发送GET请求

    本篇主要记录下使用python的requests模块发送GET请求的实现代码. 向服务器发送get请求:无参数时:r = requests.get(url)带params时:r = requests. ...

随机推荐

  1. 【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix

    题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the ...

  2. node简单起服务

    1.建一个app.js文件 const http = require('http'); const chalk = require('chalk'); const conf = require('./ ...

  3. python 多线程实现循环打印 abc

    python 多线程实现循环打印 abc 好久没写过python了, 想自己实践一下把 非阻塞版 import threading import time def print_a(): global ...

  4. C++入门经典-例9.2-重载函数模板,求出字符串的最小值

    1:整形数和实型数编译器可以直接进行比较,所以使用函数模板后也可以直接进行比较,但如果是字符指针指向的字符串该如何处理呢?这时可以通过重载函数模板来实现.通常字符串需要库函数来进行比较,通过重载函数模 ...

  5. Windows环境下Zookeeper的安装和部署(单机模式和伪集群模式)

    第一部分:单机模式 1)下载地址:http://www.pirbot.com/mirrors/apache/zookeeper/,建议下载stable版本 2)解压缩 将下载好的压缩包解压到指定目录, ...

  6. (转载)Nginx Windows详细安装部署教程

    本文转载自:https://www.cnblogs.com/taiyonghai/p/9402734.html 一.Nginx简介 Nginx (engine x) 是一个高性能的HTTP和反向代理服 ...

  7. 域名与服务器 ip地址的理解

    域名 服务器 ip地址具有怎样的关系呢 通俗的讲,我们访问一个网站就相当于访问一个服务器的文件,如果想要通过自己的域名来访问一个网站,首先得将域名部署到你的服务器上,然后就可以通过域名访问到你服务器上 ...

  8. mybatis延迟加载(assocation)

    一.何为延迟加载? 延迟加载 我们并不需要总是在加载用户信息时就一定要加载他的账户信息.此时就是我们所说的延迟加载. 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据.延迟加载也称懒加载. ...

  9. UFLDL(Unsupervised Feature Learning and Deep Learning)

    UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...

  10. JDBC——DBHelper代码模版

    JDBC数据库操作代码模版 package com.oolong.util; import java.sql.Connection; import java.sql.DriverManager; im ...