【问题标题】:How can I extract an int?我怎样才能提取一个int?
【发布时间】:2021-11-12 15:51:17
【问题描述】:

我是堆栈溢出的新手,我正在用 python 编写一个脚本,我有一个疑问我可以解决,我需要创建一个包含产品价格的变量,现在我已经收集了感谢网络抓取,以欧元为单位的十进制价格。

import bs4, requests
 
link = "https://hookpod.shop/products/hookpod-screw-adapter"
 
response = requests.get(link)
response.raise_for_status()
soup = bs4.BeatifulSoup(response.text, 'html.parster')
span_price = soup.find('span', class_='product__price')

输出给我的是:

<span class="product__price" data-product-price=""> €10.00 </span>

我需要获取金额(10.00 欧元)并将其转换为 int,有没有人可以帮助我,我真的需要它

【问题讨论】:

  • span_price = int(span_price.text.replace('€', ''))

标签: python python-3.x string web-scraping integer


【解决方案1】:

find 方法返回一个 Tag 对象,您可以通过 text 属性访问其字符串。然后,您应该使用strip 和货币符号(例如,使用切片)删除它周围的空白区域。演员阵容为float,最后是int

from bs4 import BeautifulSoup

html = '<span class="product__price" data-product-price=""> €10.00 </span>'

span_price = BeautifulSoup(html,'lxml') # you can change parser

span_price_value = int(float(span_price.text.strip()[1:]))

print(span_price_value)

备注:

  1. 我使用了另一个解析器位没有区别,如果您还没有安装它,请务必更改它 (lxml)
  2. 如果不使用strip,那么你应该小心字符串的切片,而不是更多的1

【讨论】:

    【解决方案2】:

    有几个错别字,所以我正在编写完整的代码。使用正则表达式从您已经获得的欧元价格中获取数字。

    import bs4, requests
    from bs4 import BeautifulSoup
    
    link = "https://hookpod.shop/products/hookpod-screw-adapter"
    
    response = requests.get(link)
    response.raise_for_status()
    soup = bs4.BeautifulSoup(response.text, 'html.parser')
    span_price = soup.find('span', class_='product__price')
    
    import re
    result = re.search(r'\d+', span_price.text)
    result_int = int(result.group())
    result_int
    

    【讨论】:

    • 我只看到你的代码给出了正确的结果: 10
    • 从 url 捕获的正确值是 10,00 欧元,而不是问题中所示的 10.00 欧元。所以转换成浮点数是行不通的。
    • 从 url 显示的捕获值正在创建异常。
    【解决方案3】:

    将 span_price 文本转换为 int 即可解决。

    类似:

    var int_span_price = int(span_price.text.replace('€', ''))
    

    【讨论】:

    • 你需要先转换成float tho,否则会报错
    • ValueError: invalid literal for int() 见以上评论
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案4】:

    使用 Beautiful Soup 的 tag 系统锁定该数据,并使用 soup.getText() 将其拉出。您还可以解析您在那里执行的 soup.find 方法的结果

    【讨论】:

    • 欢迎来到 StackOverflow,感谢您的回答!您能否根据原始问题提供更完整的示例?它会让未来的读者更清楚。
    【解决方案5】:

    我推荐你使用https://pypi.org/project/price-parser/

    要安装它运行pip install price-parser

    >>> from price_parser import Price
    >>> price = Price.fromstring("22,90 €")
    >>> price
    Price(amount=Decimal('22.90'), currency='€')
    >>> price.amount       # numeric price amount
    Decimal('22.90')
    >>> price.currency     # currency symbol, as appears in the string
    '€'
    >>> price.amount_text  # price amount, as appears in the string
    '22,90'
    >>> price.amount_float # price amount as float, not Decimal
    22.9
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      相关资源
      最近更新 更多