bs4 & 二进制写入图片视频
2024-09-04 23:51:38
适用于:数据都在网页源代码上,可以直接从中提取到对应数据
例子:北京新发地网
原理:拿到页面源代码的文本,交给BeautifulSoup解析,然后找到对应的标签,获取值
关键词:BeautifulSoup find find_all get / mode='wb' .content
resp = requests.get(url,headers=headers)
#print(resp.text)
#resp.encoding='utf-8'
#解析数据
#1.把页面源代码较给BeautifulSoup处理生成对象
page=BeautifulSoup(resp.text,features="html.parser") #features指定html
#2.从bs对象可以查到数据 find(找第一个) find_all(找全部的)
#find(div,属性=)
#table=page.find("table",class_='hq_table') #因为class是python关键字,他就加一个_
table=page.find("table",attrs={'class':'hq_table'}) #跟上面的是同一个含义
trs=table.find_all('tr')[1:] #trs是列表 列表里每个元素是'bs4.element.Tag'型
for tr in trs:
tds=tr.find_all('td')
xx=tds[0].text
查数据也可以用select,甚至两者可以混用
res = page.select('tr[class="tr_color"]')
xx=item.select('h3 > span[class="comment-info"] > span')[1].attrs['title']
ss= item.select('p > span')[0].text
获取标签属性值
one.get('src') #获取标签属性值
二进制写图片视频等
img_resp=requests.get(url)
with open('image/'+img_name,mode='wb') as f: #二进制写 #为了防止索引卡,可以把image目录标记为排除
f.write(img_resp.content) #图片内容写入到文件
img_resp.close()
f=open(f'video/{n}.ts',mode='wb',)
f.write(resp3.content)
f.close()
最新文章
- python 中BeautifulSoup入门
- LoadRunner中多值关联的3种处理方式
- UEditor 之查询当前编辑区域的状态是源码模式还是可视化模式
- css div要点汇总
- 一步一步学习Unity3d学习笔记系1.4单服模式架构
- HTML你应该知道的三大基本元素
- Unity3D-Baked Lightmapping 示例学习
- net core与golang web
- C语言优化实例:为了消除嵌套switch-case聪明的做法
- Linux在iptables教程基本应用防火墙
- [51nod1299]监狱逃离
- 201421123042 《Java程序设计》第11周学习总结
- python中RabbitMQ的使用(安装和简单教程)
- Java多线程编程实战读书笔记(一)
- python之路--MySQL权限管理 数据备份还原
- mysql连接拍错总结
- Linux学习笔记《六》
- 【随笔】Android应用市场搜索优化(ASO)
- AngularJS学习之 ngTable 翻页 功能以及利用angular service准备测试数据
- requirejs概念
热门文章
- TreeMap与TreeSet的源码分析
- JavaScript ajax返回状态
- 什么是bean装配?
- 转载:平衡二叉树(AVL Tree)
- 什么是 Spring Framework?
- 抽象的(abstract)方法是否可同时是静态的(static), 是否可同时是本地方法(native),是否可同时被 synchronized 修饰?
- RabbitMQ踩坑记
- eclipse开发工具之“指定Maven仓库和setting.xml文件位置”
- 使用el-tree-transfer的方式
- 手把手教你从零写一个简单的 VUE