2.Python爬虫入门_requests
2024-09-07 00:25:56
#2019-11-23
#requests的api使用非常简单
import requests
import time if __name__=='__main__':
# get请求
url_get='http://www.httpbin.org/get' #测试网站
response_get=requests.get(url=url_get)
#1.response.text 返回的是Unicode型的数据,适合得到文本
#2.response.content 返回的是bytes型的数据,适合得到图片,视频(网络编程中,服务器和浏览器只认bytes类型数据)
#2.response.headers 报文头
print('get_text\n',response_get.text)
print('get_content\n',response_get.content)
print('get_header\n',response_get.headers)
print('\n\n\n') # post请求(可以传递参数)
url_post='http://www.httpbin.org/post' #测试网站
response_post=requests.get(url=url_post,data={'name':'softpo','id':'pie'}) #以字典方式传递参数
#对于真实网站,如果参数不对,可能根本无法获取返回
#1.response_post.text
#2.response_post.content
#3.response_post.headers
print('post_text\n',response_post.text)
print('post_content\n',response_post.content)
print('post_headers\n',response_post.headers) #图片练习
url_picture='http://c.hiphotos.baidu.com/image/pic/item/6c224f4a20a44623c3f7f2649722720e0cf3d7f3.jpg'
response_picture=requests.get(url=url_picture)
#创建一个.jpg文件,以二进制的方式进行写入
with open('picture.jpg','wb') as fp:
content=response_picture.content
fp.write(content)
print('picture保存成功!') #百度贴吧 贴吧url尾号第一页0,第二页50,第三页100....
url_tieba='http://tieba.baidu.com/f?kw=%E6%9D%AD%E5%B7%9E%E7%94%B5%E5%AD%90%E7%A7%91%E6%8A%80%E5%A4%A7%E5%AD%A6&ie=utf-8&pn='
for i in range(10):
print(url_tieba+str(i*50))
response_tieba=requests.get(url_tieba)
html=response_tieba.text
with open('./TieBa/%d.html'%(i+1),mode='w',encoding='utf-8') as fp: #自己设定文件目录
fp.write(html)
print('贴吧第%d页保存成功!'%(i+1))
time.sleep(2) #如果对方有防护措施,可以使用time.sleep(n)进行休眠一段时间(n秒)
详解链接:https://blog.csdn.net/shanzhizi/article/details/50903748
最新文章
- 51nod1240(莫比乌斯函数)
- Mac系统搭建java开发环境
- mysql入门语句10条
- 【7集iCore3基础视频】7-2 iCore3原理图介绍
- Burning Bridges-ZOJ1588(割边求解)
- get mac 20150202
- Axure 快捷方式
- JMeter学习笔记-JForum环境搭建
- iOS开发工程师必备技能(持续更新)
- SpringBoot+MyBatis配置多数据源
- percona-toolkit安装
- 【CF280D】 k-Maximum Subsequence Sum ,线段树模拟费用流
- 【LeetCode每天一题】Merge Intervals(合并区间)
- React 中 keys 的作用是什么?
- 记录一则RMAN恢复到历史备份(多个incarnation)
- obv15 实例6:如果K线柱过多,ZIG将发生变动,导致明显的OBV15指标被隐藏!
- 读取文件 读取项目里面的json
- nginx安装扩展 sub_filter&;http_ssl_module
- hdu 4927 组合+公式
- BFS和DFS算法
热门文章
- SQL Server存储过程数据库日志文件备份的脚本-干货
- 并发编程 ~~~ 多进程~~~进程创建的两种方式, 进程pid, 验证进程之间的空间隔离, 进程对象join方法, 进程对象其他属性
- Redis—配置文件详解
- Day5- Python基础5 模块导入、time、datetime、random、os、sys、hashlib、json&;pickle
- Hbase启动出问题 master.HMaster: Failed to become active master
- LeetCode解题笔记 - 1. Two Sum
- react的this.setState中的坑
- dva+umi+antd项目从搭建到使用(没有剖验证,不知道在说i什么)
- autojump--懒人利器
- 对systemV和systemd的简单理解(服务方面)