怎么使用beautifulsoup获取指定div标签内容
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么使用beautifulsoup获取指定div标签内容相关的知识,希望对你有一定的参考价值。
参考技术A 一、使用BeautifulSoup解析,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
print(soup.prettify())
# <html>
# <head>
# <title>
# The Dormouse's story
# </title>
# </head>
# <body>
# <p class="title">
# <b>
# The Dormouse's story
# </b>
# </p>
# <p class="story">
# Once upon a time there were three little sisters; and their names were
# <a class="sister" href="http://example.com/elsie" id="link1">
# Elsie
# </a>
# ,
# <a class="sister" href="http://example.com/lacie" id="link2">
# Lacie
# </a>
# and
# <a class="sister" href="http://example.com/tillie" id="link2">
# Tillie
# </a>
# ; and they lived at the bottom of a well.
# </p>
# <p class="story">
# ...
# </p>
# </body>
# </html>
二、简介:
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会节省数小时甚至数天的工作时间. 参考技术B 文档地址:http://www.crummy.com/software/BeautifulSoup/bs4/doc/
很久之前写过的部分代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
f = urllib2.urlopen(url)
req = f.read()
soup = BeautifulSoup(req)
content = soup.findAll(attrs="name":"readonlycounter2")
subId = content[0].string.split(',')[1]
subName = soup.html.body.h1.span.string
content = soup.findAll(attrs="class":"subdes_td")
subType = content[0].string
subLeg = content[1].string
content = soup.findAll(attrs="colspan":"3")
subTime = content[2].string
subFile = content[7].div.string
应该可以满足你的需求 参考技术C f = urllib2.urlopen(url)
req = f.read()
soup = BeautifulSoup(req)
content = soup.findAll(attrs="name":"readonlycounter2")
subId = content[0].string.split(',')[1]
subName = soup.html.body.h1.span.string
content = soup.findAll(attrs="class":"subdes_td")
subType = content[0].string
subLeg = content[1].string
content = soup.findAll(attrs="colspan":"3")
subTime = content[2].string
subFile = content[7].div.string 参考技术D 怎么使用beautifulsoup获取指定div标签内容,例如
html="""
<html>
<body>
<div class="a">....</div>
<div class="b">i like it</div>
</body>
</html>
----------------------------
然后代码(大概)
from bs4 import BeautifulSoup
soup = BeautifulSoup(html);
content=soup.html.body.div ?? #这怎么用存在的class属性直接指定到该div下。 第5个回答 2016-02-07 f = urllib2.urlopen(url)
req = f.read()
soup = BeautifulSoup(req)
content = soup.findAll(attrs="name":"readonlycounter2")
subId = content[0].string.split(',')[1]
subName = soup.html.body.h1.span.string
content = soup.findAll(attrs="class":"subdes_td")
subType = content[0].string
subLeg = content[1].string
content = soup.findAll(attrs="colspan":"3")
subTime = content[2].string
subFile = content[7].div.string
BeautifulSoup获取指定class样式的div
如何获取指定的标签的内容是解析网页爬取数据的必要手段,比如想获取<div class=‘xxx‘> ...<div>这样的div标签,按照BeautifulSoup官方文档的说明怎么都不能成功,后来在百度知道(http://zhidao.baidu.com/question/433247968620775644.html)找到答案,真是扯淡,附上有效代码:
bs=BeautifulSoup(html) print bs.find_all(name=‘div‘,attrs={"class":"footer"})#按照字典的形式给attrs参数赋值
完整的:
from bs4 import BeautifulSoup import urllib2def getTargetDiv(url,myAttrs): html=urllib2.urlopen(url).read() bs=BeautifulSoup(html) return bs.find_all(name=‘div‘,attrs=myAttrs) if __name__=="__main__": url=r‘http://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/‘ myAttrs={‘class‘:‘footer‘} print getTargetDiv(url, myAttrs)
按照官方文档(http://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/)的做法:
#1.soup.find_all("a", class_="sister") #2.css_soup.find_all("p", class_="body") #3.soup.find_all(href=re.compile("elsie"))
改成
bs.find_all(name=‘div‘,class_=re.compile(‘info_item‘)) 或者
bs.find_all(‘div‘,class_=‘info_item‘)
都没有匹配结果,经测试需要bs.find_all(name=‘div‘,attrs={"class":"footer"})这样以字典的形式给attrs参数赋值才可以。
以上是关于怎么使用beautifulsoup获取指定div标签内容的主要内容,如果未能解决你的问题,请参考以下文章
使用 BeautifulSoup 按 id 获取 div 的内容
BeautifulSoup 之 select 总结---1205