我正在嘗試獲取 ul 標簽下所有 li 標簽中的鏈接
HTML 代碼:
<div id="chapter-list" class="sbox" style="">
<ul>
<li>
<a href="https://example.com/manga/name/2">
<div class="chpbox">
<span class="chapternum">
Chapter 2 </span>
</div>
</a>
</li>
<li>
<a href="https://example.com/manga/name/1">
<div class="chpbox">
<span class="chapternum">
Chapter 1 </span>
</div>
</a>
</li>
</ul>
</div>
我寫的代碼:
from bs4 import BeautifulSoup
import requests
html_page = requests.get('https://example.com/manga/name/')
soup = BeautifulSoup(html_page.content, 'html.parser')
chapters = soup.find('div', {"id": "chapter-list"})
children = chapters.findChildren("ul" , recursive=False) # when printed, it gives the the whole ul content
for litag in children.find('li'):
print(litag.find("a")["href"])
當我嘗試列印 li 標簽鏈接時,會出現以下錯誤:
Traceback (most recent call last):
File "C:\0.py", line 12, in <module>
for litag in children.find('li'):
File "C:\Users\hs\AppData\Local\Programs\Python\Python310\lib\site-packages\bs4\element.py", line 2289, in __getattr__
raise AttributeError(
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
uj5u.com熱心網友回復:
您可以使用在章節串列中find查找。ul然后find_all在ul. 最后,find_all再次使用查找每個串列項中的鏈接并列印 URL。這兩種方法的詳細資訊可以在bs4 上的find 和 find_all 方法檔案中找到。您可以在每個鏈接上使用get_text()按類搜索后的chapternum鏈接來獲取鏈接的文本,例如Chapter 1. 按類搜索可在bs4 檔案中找到按類搜索元素
(更新)代碼:
from bs4 import BeautifulSoup
html_doc = """
<div id="chapter-list" style="">
<ul>
<li>
<a href="https://example.com/manga/name/2">
<div >
<span >
Chapter 2 </span>
</div>
</a>
</li>
<li>
<a href="https://example.com/manga/name/1">
<div >
<span >
Chapter 1 </span>
</div>
</a>
</li>
</ul>
</div>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
chapters = soup.find('div', {"id": "chapter-list"})
list_items = chapters.find('ul').find_all('li')
for list_item in list_items:
for link in list_item.find_all('a'):
title = link.find('span', class_='chapternum').get_text().strip()
href = link.get("href")
print(f"{title}: {href}")
輸出:
Chapter 2: https://example.com/manga/name/2
Chapter 1: https://example.com/manga/name/1
參考:
- bs4 上的 find 和 find_all 方法檔案
- 按類搜索元素的 bs4 檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/492296.html
上一篇:Golang表格抓取
下一篇:為什么這會列印一個空串列和字典?
