以下代码python3.7版本测试通过。beautifulsoup使用select、find_all等方法可以十分简便地获取到想要的结果。多层div查找,指定class或id查找。
from bs4 import BeautifulSouphtml="""<head><title>瑞典取经中国“火眼”实验室:目前可日测万例_网易科技</title><meta name="keywords" content="火眼,瑞典,病毒,高通量,通量"><link rel="stylesheet" href="https://static.ws.126.net/163/f2e/post_nodejs/css/index.css?v=8"><style>#endText p img {max-width: 550px;}</style></head><body><div class="post_content_main" id="epContentLeft"><h1>瑞典取经中国“火眼”实验室:目前可日测万例</h1><!-- 来源 --><!-- 来源 --><div class="post_time_source">2020-03-28 08:57:16 来源: <a id="ne_article_source" href="http://www.thepaper.cn/?" target="_blank">澎湃新闻</a><a href="http://jubao.aq.163.com/" target="_blank" class="post_jubao" title="举报">举报</a></div><div class="post_body"><div class="post_text" id="endText" style="border-top:1px solid #ddd;"><p>近日,华大集团旗披露将在瑞典首都斯德哥尔摩共建万人级别新冠检测多组学检测实验室。</p><p>该实验室集荧光定量核酸检测、高通量测序、抗体三种方法于一体。</p><p>除武汉之外,华大基因在深圳、天津、长沙、石家庄、北京、无锡(技术支持)等地布局。</p><p>他认为,快速给出临床诊断需要及时精准的检测。</p><p></p><div class="ep-source cDGray"><span class="left"><a href="http://tech.163.com/"><img src="https://static.ws.126.net/cnews/css13/img/end_tech.png" alt=" 丁广胜" width="13" height="12" class="icon"></a> 本文来源:澎湃新闻 </span><span class="ep-editor">责任编辑:丁广胜_NT1941</span></div></div></div></body>"""#print(html)soup = BeautifulSoup(html, "lxml")# 通过标签名查找#list = soup.select('div')# 查找所有p标签。list = soup.select('p')#print(len(list)) # list的个数===5#print(list)#[<p>近日,华大集团旗披露将在瑞典首都斯德哥尔摩共建万人级别新冠检测多组学检测实验室。</p>, <p>该实验室集荧光定量核酸检测 、高通量测序、抗体三种方法于一体。</p>, <p>除武汉之外,华大基因在深圳、天津、长沙、石家庄、北京、无锡(技术支持)等地布 局。</p>, <p>他认为,快速给出临床诊断需要及时精准的检测。</p>, <p></p>]#获取list串第一个字符串 中的文本内容(自动清除标签)list0_str=list[0].get_text()#print(list0_str)#近日,华大集团旗披露将在瑞典首都斯德哥尔摩共建万人级别新冠检测多组学检测实验室。#查找位于head 标签下的title标签list = soup.select('head>title') #等同于 list = soup.select('head title')#print(list)#[<title>瑞典取经中国“火眼”实验室:目前可日测万例_网易科技</title>]#基本选择器输出title内容list = soup.title.string#print(soup.title.name)#获取标签的名称#print(list)#瑞典取经中国“火眼”实验室:目前可日测万例_网易科技#通过标类名查找 class="ep-editor"list = soup.select('.ep-editor')#print(list)#[<span class="ep-editor">责任编辑:丁广胜_NT1941</span>]##获取list串第一个字符串 中属性class的值, get('属性') 一般也用来获取href标签地址、图片alt描述等list0_class =list[0].get("class")#print(list0_class)#['ep-editor']#通过id查找,id="epContentLeft"的标签及内容listlist = soup.select('#epContentLeft')#print(list)# <div class="post_time_source">……………………</div>#组合查找list = soup.select('head style')#print(list)#[<style>#endText p img {max-width: 550px;}</style>]#获取第4层div内容list = soup.select('div div div div')#print(list)#[<div class="ep-source cDGray">…………</div>]#h获取第4成div标签下的img 标签列表----第一个字符串------src属性的值list = soup.select('div div div div img')#print(list[0].get('src'))#https://static.ws.126.net/cnews/css13/img/end_tech.png#查找第二层div标签中div class属性=post_time_source的listlist = soup.select('div div[class="post_time_source"]')#print(list)#[<div class="post_time_source"> ……………………</div>]##查找定制class的div标签中再往子节点进行查找a标签list = soup.select('div div[class="post_time_source"] a')#print(list[0].get('href'))#http://www.thepaper.cn/?###list = soup.select('span[class="ep-editor"]').parent##print(list)###查找id#print(soup.find_all(attrs={'id': 'epContentLeft'}))#print(soup.find_all(id='epContentLeft'))#查找属性及值#print(soup.find_all(attrs={'class': 'post_text'}))#print(soup.find_all(class_ ='post_text'))#print(soup.find_all('title'))##div 多个class属性#print(soup.find_all("div", {"class":{"post_time_source", "class2"}}))##注意,这里text后面的是标签文本完全匹配,它不是模糊搜索。#<span class="ep-editor">责任编辑:丁广胜_NT1941</span> 下面是<span>标签内容为'责任编辑:丁广胜_NT1941'的完全匹配个数#print(soup.find_all(text='责任编辑')) 这种查找方式是没有结果的!!print(soup.find_all(text='责任编辑:丁广胜_NT1941'))#['责任编辑:丁广胜_NT1941']
发表评论