<html>
<head>
<title>The Dormouse's story </title>
</head>
<body>
<p id="p1">p1p1p1
<b id='b1'>b1b1b1</b>
</p>
<p id="p2">p2p2p2
<ul id='u1'>u1u1u1</ul>
<a id="a1">a1a1a1</a>
<p id='d1'>
<a id="a2">a2a2a2 </a>
<b id='b2'>b2b2b2</b>
<p id='p3'>p3p3p3</p>
</p>
<a id="a3">a3a3a3 </a>
</p>
<p id="p4">p4p4p4</p>
</body>
</html>
比如第一个a元素:a#a1,要获取这个元素以上的所有网页源码:
<html>
<head>
<title>The Dormouse's story </title>
</head>
<body>
<p id="p1">p1p1p1
<b id='b1'>b1b1b1</b>
</p>
<p id="p2">p2p2p2
<ul id='u1'>u1u1u1</ul>
<a id="a1">a1a1a1</a>
</p>
</body>
</html>
Since your original html is not up to standard, I changed it a bit.
The following is made using lxml.
Use bs4 to extract
Newbie, I only learned the re module, so I only use the re module + the normal way to extract
be module is the most convenient