[英]How to extract the strong data from div tag?
<div class="type"><span class="tag_ts" title="Time sale">Time sale</span></div><del>$35.90</del><strong title="Discounted Price">$12.90</strong>
这是我的代码
from bs4 import BeautifulSoup as soup from urllib.request import Request,urlopen myurl=Request('https://www.qoo10.sg/gmkt.inc/search/CategoryImageSearch.aspx?choice_no=569', headers={'User-Agent': 'Mozilla/5.0'}) pagehtml=urlopen(myurl).read() pagesoup=soup(pagehtml,'html.parser') containers=pagesoup.find_all('div',{'class':'item_wrap'}) container=containers[0] for container in containers: prdt_price=container.find_all('div',{'class':'prc'}) price=prdt_price[0].text print(price)
我的输出是:定期出售$ 35.90 $ 12.90
我需要得到$ 12.90
你只需要改变
prdt_price=container.find_all('div',{'class':'prc'})
至
prdt_price = container.find_all('strong')
要使文本脱离强元素,(如果您确定容器中的所有元素都具有强元素),则可以使用container.strong.text
要仅获取强文本,请使用类似以下内容的内容:
for container in containers:
prdt_price=container.find_all('div',{'class':'prc'})
price=prdt_price[0].strong.text
print(price)
或者,您可以将其减少为:
for container in containers:
print(container.find_all('div',{'class':'prc'})[0].strong.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.