从div中获得价值,还有另一个空白标签python beautifulsoup

发布于 2025-02-10 22:08:17 字数 1378 浏览 1 评论 0原文

我正在尝试从以下内容中获取值的“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软的没边 2025-02-17 22:08:17

不知道为什么您会遇到错误。由于某种原因,我看不到它被阻止的原始站点,但是可以使用您输入的DIV运行Express Server,并且在以下情况下使用以下功能对我来说很好。

import string

import bs4
import requests

if __name__ == '__main__':
    r = requests.get('http://localhost:3000/')
    soup = bs4.BeautifulSoup(r.text)
    div = soup.find('div', class_='DrugPriceBox__price___dj2lv')
    acceptable_chars = set(string.ascii_letters + string.digits + '.')
    drugprice = ''.join(char for char in div.text if char in acceptable_chars)
    print(drugprice)

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.

import string

import bs4
import requests

if __name__ == '__main__':
    r = requests.get('http://localhost:3000/')
    soup = bs4.BeautifulSoup(r.text)
    div = soup.find('div', class_='DrugPriceBox__price___dj2lv')
    acceptable_chars = set(string.ascii_letters + string.digits + '.')
    drugprice = ''.join(char for char in div.text if char in acceptable_chars)
    print(drugprice)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文