首页 > 其他 > 详细

用简单的例子说明BeautifulSoup库的使用

时间:2019-09-01 12:56:02      阅读:71      评论:0      收藏:0      [点我收藏+]
"ISO-8859-1BeautifulSoup库,为python外库,使用前要先安装,主要功能就是用来解析HTML代码,挖出我们相要的数据
下面是一个例子,拿到500万网站的近30期双色球的开奖号码

import requests
from bs4 import BeautifulSoup  #引入库
url="https://datachart.500.com/ssq/?expect=100"
re=requests.get(url)
if re.status_code==200:  #表示请求成功,可以继续往下面走
if re.encoding=="ISO-8859-1":
html=re.text.encode("ISO-8859-1").decode("GBK") #ISO-8859-1要转码,不然中文有乱码
else:
html=re.text #utf-8编码直接用,
soup=BeautifulSoup(html, ‘lxml‘)    #没安装lxml的要安装,不然要报错
#获得期号
re_id=soup.tbody.findAll("td",attrs={"align":"center"})    #属性选择器
qihao=[x.string for x in re_id] #[x.get_text() for x in re_id]都可以,string是去掉标签,获得内容
print(qihao) #[‘19072 ‘, ‘19073 ‘, ‘19074 ‘, ‘19075 ‘, ...]
#获得红与蓝球
re_red=soup.tbody.tr.findAll("td",attrs={"class":"chartBall01"})  #通过属性选择器获得红球代码块
re_blue=soup.tbody.tr.findAll("td",attrs={"class":"chartBall02"})  #通过属性选择器获得蓝球代码块
#再将标签去掉,得到纯内容
r=[x.string for x in re_red] #python列表生成式,不用多说
b=[x.string for x in re_blue]
newr=r[-7:-1]  #用切片获得最新一期红球,
newb=b[-1] #最新一期的蓝球

#拿到自己相要的数据了,下面就是数据的整理,分析...

#下面再帖点findAll的一些用法
soup.find_all("p")
soup.find_all("title")
soup.find_all(id="link2")
soup.find_all(id="link2",limit=2)
soup.find_all(id=True)
soup.find_all(id=True)
soup.find_all("a",class_="classname")
soup.find_all(text="文本内容")
soup.find_all(text=["tanghao","laowang"])
soup.find_all("a",attrs={"class":"classname"})
soup.select("p.title")

用简单的例子说明BeautifulSoup库的使用

原文:https://www.cnblogs.com/yiyea/p/11441628.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!