在 Beautiful Soup 中,標簽或字符串的 next_elements
屬性返回一個生成器,用於迭代解析樹中的所有下一個字符串或標簽。
例子
考慮以下 HTML 文檔:
my_html = """
<p>Alex</p>
<p>Bob</p>
"""
soup = BeautifulSoup(my_html)
讓我們獲取 <p>Alex</p>
的 next_elements
並循環生成生成器:
p_alex = soup.find("p")
for element in p_alex.next_elements:
print(element)
<p>Alex</p>
Alex
<p>Bob</p>
Bob
請注意內部字符串如何注冊為下一個元素。這裏的要點是順序是從標簽到內部字符串。此外,<p>Alex</p>
和 <p>Bob</p>
之間有一個新行字符 \n
,這就是我們在輸出中看到空行的原因。
相關用法
- Python BeautifulSoup next_element屬性用法及代碼示例
- Python BeautifulSoup next_sibling屬性用法及代碼示例
- Python BeautifulSoup next_siblings屬性用法及代碼示例
- Python next方法用法及代碼示例
- Python next()用法及代碼示例
- Python networkx.algorithms.shortest_paths.weighted.all_pairs_dijkstra_path用法及代碼示例
- Python networkx.classes.function.edge_subgraph用法及代碼示例
- Python networkx.algorithms.tree.mst.maximum_spanning_edges用法及代碼示例
- Python networkx.algorithms.bipartite.basic.color用法及代碼示例
- Python networkx.algorithms.bipartite.cluster.latapy_clustering用法及代碼示例
- Python networkx.readwrite.json_graph.adjacency_data用法及代碼示例
- Python networkx.algorithms.bipartite.edgelist.generate_edgelist用法及代碼示例
- Python networkx.Graph.neighbors用法及代碼示例
- Python networkx.DiGraph.__contains__用法及代碼示例
- Python networkx.drawing.nx_pylab.draw_random用法及代碼示例
- Python networkx.convert_matrix.to_pandas_edgelist用法及代碼示例
- Python networkx.DiGraph.__init__用法及代碼示例
- Python networkx.algorithms.cycles.recursive_simple_cycles用法及代碼示例
- Python networkx.drawing.layout.random_layout用法及代碼示例
- Python networkx.algorithms.link_prediction.within_inter_cluster用法及代碼示例
- Python networkx.algorithms.non_randomness.non_randomness用法及代碼示例
- Python networkx.algorithms.traversal.depth_first_search.dfs_tree用法及代碼示例
- Python networkx.MultiGraph.clear用法及代碼示例
- Python networkx.algorithms.operators.product.cartesian_product用法及代碼示例
- Python networkx.algorithms.traversal.depth_first_search.dfs_labeled_edges用法及代碼示例
注:本文由純淨天空篩選整理自Arthur Yanagisawa大神的英文原創作品 BeautifulSoup | next_elements property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。