for link in soup.findAll('li'):
if "c-listing__authors-list" in str(link):
# theAuthor = link.string
theAuthor = str(link).replace("</p>","")
theAuthor = theAuthor.split("</span>")[1]
listAuthor.append(theAuthor)[Output][1]

uj5u.com熱心網友回復:
嘗試使用get_text(strip=True)來實作您的目標:
for e in soup.select('li span.c-listing__authors-list'):
theAuthor = e.get_text(strip=True)
或在一行中獲取串列:
theAuthor = [e.get_text(strip=True) for e in soup.select('li span.c-listing__authors-list')]
例子
from bs4 import BeautifulSoup
html='''
<ul>
<li><span >a</span></li>
<li><span >b</span></li>
<li><span>no list</span></li>
</ul>
'''
soup = BeautifulSoup(html)
theAuthor = []
for e in soup.select('li span.c-listing__authors-list'):
theAuthor.append(e.get_text(strip=True))
輸出
['a', 'b']
uj5u.com熱心網友回復:
這個答案以 Microsoft (.Net) 為中心,但我希望它可以幫助您指出正確的方向。
自從我創建刮板以來已經有一段時間了。但是我認為如果您也知道您的 XPath,這是可能的,因為我記得能夠將網頁讀入 HTMLDocument,使用 XPath 訪問您需要的元素,然后獲取它的文本值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/484307.html
