Heim > Fragen und Antworten > Hauptteil
遇到一个平级标签的页面,如下显示:
<!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'>
就不知道要怎么取了。
问大神给指点一下。
PHP中文网2017-04-17 17:33:51
h2_a = soup.find_all('h2')
for i_a in h2_a:
if i_a.a:
print (i_a.text,',',i_a.a['href'])
else:
print (i_a.text)
python3下的。python2的print不知道怎么写了,不知是否符合你的要求
PHP中文网2017-04-17 17:33:51
I can't spell Chinese for the bad OS.
I thinke that we can solve this question using re
.
import re
resList = b = re.findall(r'<h2>(.*?)</h2>([\w\W]*?)(?=((<h2>)|(</body>)))',html.replace('\n',''))
then: suppose a
in resList
, a[0]
is the parent title, and a[1]
is the sub content.
try it.