遇到一个平级标签的页面,如下显示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>1. 测试标题一</h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/1374.html" target="_blank">测试一小标题1</a></h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/1410.html" target="_blank">测试一小标题2</a></h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/1520.html" target="_blank">测试一小标题3</a></h2>
<h2>2. 测试标题二</h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/779.html" target="_blank">测试二小标题1</a></h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/842.html" target="_blank">测试二小标题2</a></h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/997.html" target="_blank">测试二小标题3</a></h2>
<h2>3. 测试标题三</h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/2301.html" target="_blank">测试三小标题1</a></h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/1976.html" target="_blank">测试三小标题2</a></h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/1905.html" target="_blank">测试三小标题3</a></h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/2440.html" target="_blank">测试三小标题4</a></h2>
<h2>4. 测试标题四</h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/1722.html" target="_blank">测试四小标题1</a></h2>
<h2 class="lesson-info-h2"><a href="http://www.xxx.xxx.com/1518.html" target="_blank">测试四小标题2</a></h2>
</body>
</html>
我最终要取得的值是
测试标题一
测试一小标题1,小标题1的链接
测试一小标题2,小标题2的链接
...
测试标题四
测试四小标题1,小标题1的链接
测试四小标题2,小标题1的链接
我原本使用的是
h2_a = soup.find_all('h2')
for i_a in h2_a:
print i_a
这样是可以把需要的h2取到,但想要在继续循环去那些小标题时候,由于得到type(i_a)为<class 'bs4.element.Tag'>
就不知道要怎么取了。
问大神给指点一下。
Under python3. I don’t know how to write python2’s print. I wonder if it meets your requirements
I thinke that we can solve this question using
re
.then: suppose
a
inresList
,a[0]
is the parent title, anda[1]
is the sub content.try it.
soup.find_all('h2', class_=None)
This way you can directly find what you need.