![](/img/trans.png)
[英]Extract text from within div tag using BeautifulSoup 4 in Python
[英]“Extract text within div tag” but gives error
這是一段代碼,我想提取文本“ Available at Amazon starting at $200"
售”。這是我到目前為止所寫的
import re
Cost_List = []
for result in stroller_result:
if result.find('div', class_ = 'product__retailer__text') is not None:
Cost = result.find_all('div', {'class': ["product__retailer__text", re.compile(r'\$\d+(?:\.\d+)?')]})
Cost_List.append(Cost)
此代碼返回:
[[<div class="product__retailer__text">Available at Amazon starting at $200</div>],
[<div class="product__retailer__text">Available at Amazon starting at $200</div>],
[<div class="product__retailer__text">Available at Amazon starting at $500</div>],
[<div class="product__retailer__text">Available at Amazon starting at $100</div>],
[<div class="product__retailer__text">Available at Amazon starting at $100</div>],
如果我嘗試提取文本,我確實會收到此錯誤: 'Available at Amazon starting at $200' using.text I get the following error - AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
'Available at Amazon starting at $200' using.text I get the following error - AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
import re
Cost_List = []
for result in stroller_result:
if result.find('div', class_ = 'product__retailer__text') is not None:
Cost = result.find_all('div', {'class': ["product__retailer__text", re.compile(r'\$\d+(?:\.\d+)?')]}).text
Cost_List.append(Cost)
為了擺脫你的錯誤,你可以在每個元素上調用.text
,而不是在元素列表上。
import re
Cost_List = []
for result in stroller_result:
if result.find('div', class_ = 'product__retailer__text') is not None:
Cost = result.find_all('div', {'class': ["product__retailer__text", re.compile(r'\$\d+(?:\.\d+)?')]})
Cost = [el.text for el in Cost]
Cost_List.append(Cost)
此代碼有效:
import re
Cost_List = []
for result in stroller_result:
if result.find('div', class_ = 'product__retailer__text') is not None:
Cost = [x.text for x in result.find_all('div', {'class': ["product__retailer__text", re.compile(r'\$\d+(?:\.\d+)?')]})]
Cost_List.append(Cost)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.