繁体   English   中英

如何从div标签中提取强数据?

[英]How to extract the strong data from div tag?

我正在使用Python抓取数据。 有人可以帮助我如何使用python从div中提取强大的数据:
 <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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM