我從一個網站使用這條線
offers = soup.find_all("span", "rcnt")
這給了我這樣的結果刮信息:
[<span class="rcnt">8.668</span>]
出於某種原因,當我試圖解開它,它給了我這個
[<span class="rcnt"></span>]
而不是8.668
Python的Beautifulsoup4刪除<span>標籤
如何正確地編寫這個
我從一個網站使用這條線
offers = soup.find_all("span", "rcnt")
這給了我這樣的結果刮信息:
[<span class="rcnt">8.668</span>]
出於某種原因,當我試圖解開它,它給了我這個
[<span class="rcnt"></span>]
而不是8.668
Python的Beautifulsoup4刪除<span>標籤
如何正確地編寫這個
從描述中不清楚您使用什麼代碼來獲取(解包)內容。這是你做的。
offers
是一個列表。爲了讓你做span元素中的內容:
elements = [tag.text for tag in offers]
elements
將所有的跨度標籤的內容在你的HTML。
>>> html = '<span class="rcnt">8.668</span><span class="rcnt">5.7868</span>'
>>> soup = BeautifulSoup(html)
>>> offers = soup.find_all("span", "rcnt")
>>> elements = [tag.text for tag in offers]
>>> elements
[u'8.668', u'5.7868']
使用.string
或.renderContents()
來獲取該值。
htmls = '<span class="rcnt">8.668</span>'
soup = BeautifulSoup(htmls)
offers = soup.find_all("span", "rcnt")
print offers[0].string ## this one is better
print offers[0].renderContents()
只需使用.string()檢索任何html標記內的值。
html = '<span class="rcnt">8.668</span>'
soup = BeautifulSoup(html)
offers = soup.('span',attrs={"class":"rcnt"})
它返回所有的跨度tag.Now你可以使用.string()函數的跨度標籤作爲內檢索串部的排列:
for i in range(0,len(offers)):
print offers[i]
非常感謝這解決了我的問題 – Carbonemys