python 爬虫 基于requests模块的get请求
需求:爬取搜狗首页的页面数据
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请求的更多相关文章
- python 爬虫 基于requests模块发起ajax的post请求
基于requests模块发起ajax的post请求 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定某个城市地点的餐厅数据 点击肯德基餐厅查 ...
- python 爬虫 基于requests模块发起ajax的get请求
基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面的请求 鼠标往下 ...
- Python爬虫之requests模块(1)
一.引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃 ...
- python爬虫值requests模块
- 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在 ...
- Python爬虫练习(requests模块)
Python爬虫练习(requests模块) 关注公众号"轻松学编程"了解更多. 一.使用正则表达式解析页面和提取数据 1.爬取动态数据(js格式) 爬取http://fund.e ...
- 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块
孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...
- Python 爬虫二 requests模块
requests模块 Requests模块 get方法请求 整体演示一下: import requests response = requests.get("https://www.baid ...
- Python爬虫之requests模块(2)
一.今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 二.回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 三. ...
- Python接口测试-使用requests模块发送GET请求
本篇主要记录下使用python的requests模块发送GET请求的实现代码. 向服务器发送get请求:无参数时:r = requests.get(url)带params时:r = requests. ...
随机推荐
- Codeforces Round #427 (Div. 2) [ C. Star sky ] [ D. Palindromic characteristics ] [ E. The penguin's game ]
本来准备好好打一场的,然而无奈腹痛只能带星号参加 (我才不是怕被打爆呢!) PROBLEM C - Star sky 题 OvO http://codeforces.com/contest/835/p ...
- Servlet中的请求转发RequestDispatcher接口的forword与Include的区别
RequestDispatcher接口中具有两个方法: forward() 与 include() 均 可完成请求 的转发.区别如下: forword(): 使用该方法,则当前 的 Servlet 中 ...
- mac 建立 .babellrc 格式文件
打开MAC的终端: 1 先sudo -s获取最高权限: 2 然后通过cd进入项目所在的路径: 3 接着“vim .babelrc”新建一个.babelrc 文件: 键盘输入”i“进入编辑模式, ...
- flask框架(四):通过局域网访问网站
一:开启局域网访问 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000) # 设置成局域网访问 二:设置windows的入站规则 ...
- codeforces gym #101873B. Buildings(Polya定理)
参考博客: https://blog.csdn.net/liangzhaoyang1/article/details/72639208 题目链接: https://codeforces.com/gym ...
- template模板循环嵌套循环
template嵌套循环写法:在第一次循环里面需要循环的地方再写个循环,把要循环的数据对象改为第一层的循环对象别名 //template模板循环嵌套循环 <script id="ban ...
- dom4j读写XML文档
dom4j 最常用最简单的用法(转) 要使用dom4j读写XML文档,需要先下载dom4j包,dom4j官方网站在 http://www.dom4j.org/目前最新dom4j包下载地址:http:/ ...
- 连接数据库出现The server time zone value '�й���ʱ��' is unrecogni等问题的解决方案
使用JDBC连接数据库出现The server time zone value '�й���ʱ��' is解决方案 ** 将jdbc.properties中url后加入?serverTimezone ...
- JVM 监控工具——jstack
[参考文章]:jstack 命令使用经验总结 1. 简介 jstack主要用于生成java虚拟机当前时刻的线程快照. 线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合, 主要目的是定位 ...
- 基本CSS布局三
基本CSS布局三------图片视频网格 <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu ...