python 调用xlrd 读取excel 内容BeautifulSoup获取html特定标签的属性

需求:有一个excel 中只有一列 八百多行,内容为html代码,要从中取出所有的img 标签的所有src 地址

python 读取excel 插件很多,最简单常用的还是xlrd xlwt 这连个,两个互补,xlrd读取,xlwt 写入

从网上找到一张操作excel的插件对比表

xlrd 读取excel 的代码如下,xlrd 只能操作xls文件,xlsx文件会报错

wb = xlrd.open_workbook(filename="00excel/list.xls")
table=wb.sheet_by_index(0)
nrows111 = table.nrows                 #行数
ncols111 = table.ncols                 #列数

读取到内容之后 调用BeautifulSoup进行html 解析,获取img 的src地址

soup = bs(con, 'html.parser')
#imglist=soup.find_all('img')
for link in soup.find_all("img"):
	print(link['src'])

国内pip 安装插件的时候会提示服务器拒绝访问啥的 ,可以使用国内的镜像源

1.安装xlwt

pip install -i https://pypi.douban.com/simple xlwt

 

2.安装 BeautifulSoup

pip install -i https://pypi.douban.com/simple bs4

完整代码如下

import xlrd
import re
from bs4 import BeautifulSoup as bs



wb = xlrd.open_workbook(filename="00excel/list.xls")
table=wb.sheet_by_index(0)
nrows111 = table.nrows                 #行数
ncols111 = table.ncols                 #列数

#print(table)
print("行数")
print(nrows111)
print("列数")
print(ncols111)
con=""
for rowi in range(nrows111):   
	#print(table.row(rowi))#按行打印数据
	con=con+table.cell(rowi,0).value


#print(con)



soup = bs(con, 'html.parser')
#imglist=soup.find_all('img')
for link in soup.find_all("img"):
	print(link['src'])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值