beautifulsoup兄弟标签
时间: 2023-10-20 07:33:12 浏览: 84
在BeautifulSoup中,可以通过使用next_siblings()方法来处理兄弟标签。该方法可以返回指定标签的后面的所有兄弟标签。例如,对于一个包含带标题行的表格的网页,可以使用next_siblings()方法来处理兄弟标签。下面是一个示例代码:
```python
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html, "html.parser")
for sibling in bsObj.find("table",{"id":"giftList"}).tr.next_siblings:
print(sibling)
```
通过以上代码,可以获取到表格中标题行之后的所有兄弟标签。每个兄弟标签都会被打印出来。请注意,next_siblings()方法返回的是一个生成器对象,所以可以使用for循环来遍历兄弟标签。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [使用BeautifulSoup爬取想要的标签(《python网络爬虫权威指南》笔记)](https://download.csdn.net/download/weixin_38698590/13742213)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [python库学习笔记——BeautifulSoup处理子标签、后代标签、兄弟标签和父标签](https://blog.csdn.net/weixin_30576859/article/details/95614705)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文