从div中获得价值,还有另一个空白标签python beautifulsoup
我正在尝试从以下内容中获取值的“ 5”:
<div class="DrugPriceBox__price___dj2lv">₹<!-- -->5</div>
我使用的python-beautifulsoup代码尚无:
drugprice=soup.find('div', class_="DrugPriceBox__price___dj2lv")
print(drugprice)
网页url是: https://www.1mg.com/drugs/acticort-5mg-tablet-321932
预先感谢您!
其他信息:
解决问题后的工作代码:
if __name__ == '__main__':
#turl='https://www.1mg.com/drugs/acticort-5mg-tablet-321932'
turl='https://www.1mg.com/drugs/zerodol-sp-tablet-67307'
print (turl)
soup = BeautifulSoup(requests.get(turl,headers=headers).content, "html.parser")
#type 'div' pricing format
div = soup.find('div', class_='DrugPriceBox__price___dj2lv')
if div:
print(div.text)
else:
#type 'span' pricing format
#span =soup.find('span', class_="PriceBoxPlanOption__offer-price___3v9x8 PriceBoxPlanOption__offer-price-cp___2QPU_")
span =soup.find('span', class_="PriceBoxPlanOption__margin-right-4___2aqFt PriceBoxPlanOption__stike___pDQVN")
if span:
print(span.text)
else:
print('Nada')
I am trying to get the value '5' of from the following :
<div class="DrugPriceBox__price___dj2lv">₹<!-- -->5</div>
and the python-beautifulsoup code i used returns Nothing:
drugprice=soup.find('div', class_="DrugPriceBox__price___dj2lv")
print(drugprice)
The webpage url is: https://www.1mg.com/drugs/acticort-5mg-tablet-321932
Thank you in advance!
Additional information:
The WORKING CODE after solving the problem:
if __name__ == '__main__':
#turl='https://www.1mg.com/drugs/acticort-5mg-tablet-321932'
turl='https://www.1mg.com/drugs/zerodol-sp-tablet-67307'
print (turl)
soup = BeautifulSoup(requests.get(turl,headers=headers).content, "html.parser")
#type 'div' pricing format
div = soup.find('div', class_='DrugPriceBox__price___dj2lv')
if div:
print(div.text)
else:
#type 'span' pricing format
#span =soup.find('span', class_="PriceBoxPlanOption__offer-price___3v9x8 PriceBoxPlanOption__offer-price-cp___2QPU_")
span =soup.find('span', class_="PriceBoxPlanOption__margin-right-4___2aqFt PriceBoxPlanOption__stike___pDQVN")
if span:
print(span.text)
else:
print('Nada')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道为什么您会遇到错误。由于某种原因,我看不到它被阻止的原始站点,但是可以使用您输入的DIV运行Express Server,并且在以下情况下使用以下功能对我来说很好。
Not sure why you are getting an error. I cannot see the original site as it is blocked for some reason, but running an express server with exactly the div that you entered, and using the below worked fine for me with the below.