【问题标题】:Beautifulsoup is pulling the rounded decimals (whats visible) from table and not actual cell valuesBeautifulsoup 正在从表格中提取四舍五入的小数(可见的),而不是实际的单元格值
【发布时间】:2019-07-02 20:11:26
【问题描述】:

试图从网页表中提取数据。页面上显示的数据是四舍五入的小数点后 3 位,但实际单元格值是小数点后 4 位。我需要完整的、未四舍五入的数字。

我的循环:

for i in range(0,20):
    soup = BeautifulSoup(html_source,'lxml')
    table = soup.find_all('table')[i]
    df = pd.read_html(str(table))
    print(region,i)
    print( tabulate(df[0], headers='keys', tablefmt='psql') )

网页元素:

 <span class="price-data " data-amount="{&quot;regional&quot;: 
 {&quot;asia-pacific-east&quot;:0.022,&quot;japan- 
 east&quot;:0.0176,&quot;japan-west&quot;:0.0206,&quot;us- 
 west&quot;:0.0164,&quot;us-west-2&quot;:0.0144,&quot;us-west- 
 central&quot;:0.018,&quot;west-india&quot;:0.0193}}" data-decimals="3" 
 data-decimals-force="3" data-month-format="{0}/month" data-hour-format=" 
 {0}/hour" data-region-unavailable="N/A" data-has-valid- 
 price="true">$0.018/hour</span>

我的代码显示0.018/hour,我需要它显示0.0176/hour

注意:这是针对 japan-east(样本数据也有 japan-west)。

【问题讨论】:

  • 原因是因为您正在提取 html 中显示的文本/内容。您必须查看并查看是否可以提取原始源并对其进行解析。
  • region 是什么?它没有在您的代码中定义。我看不到您的代码如何打印任何类似 0.018/小时的内容 .. 您 pd.read_html(str(table)) 一个表属性,但您没有显示表数据 - 只有一个跨度 ...
  • 这个网址是什么?

标签: python selenium web-scraping beautifulsoup


【解决方案1】:

假设 JSON 格式正确,您可以从 &lt;span&gt; 中的 data-amount 属性中提取它,如下所示:

from bs4 import BeautifulSoup
import html
import json

html_text = """<span class="price-data " data-amount="{&quot;regional&quot;:{&quot;asia-pacific-east&quot;:0.022,&quot;japan-east&quot;:0.0176,&quot;japan-west&quot;:0.0206,&quot;us-west&quot;:0.0164,&quot;us-west-2&quot;:0.0144,&quot;us-west-central&quot;:0.018,&quot;west-india&quot;:0.0193}}" data-decimals="3" data-decimals-force="3" data-month-format="{0}/month" data-hour-format="{0}/hour"data-region-unavailable="N/A" data-has-valid-price="true">$0.018/hour</span>"""

soup = BeautifulSoup(html_text, "html.parser")
da = html.unescape(soup.span['data-amount'])
data_amount = json.loads(da)

print(data_amount['regional']['japan-east'])

将显示的内容:

0.0176

【讨论】:

    【解决方案2】:

    您还可以如图所示更正json并使用以下内容

    from bs4 import BeautifulSoup
    import json
    html = '''<span class="price-data " data-amount="{&quot;regional&quot;: 
         {&quot;asia-pacific-east&quot;:0.022,&quot;japan- 
         east&quot;:0.0176,&quot;japan-west&quot;:0.0206,&quot;us- 
         west&quot;:0.0164,&quot;us-west-2&quot;:0.0144,&quot;us-west- 
         central&quot;:0.018,&quot;west-india&quot;:0.0193}}" data-decimals="3" 
         data-decimals-force="3" data-month-format="{0}/month" data-hour-format=" 
         {0}/hour" data-region-unavailable="N/A" data-has-valid- 
         price="true">$0.018/hour</span>'''
    
    soup = BeautifulSoup(html,'lxml')
    items = soup.select('span.price-data')
    for item in items:
        if item.has_attr('data-amount'):
            val = json.loads(item['data-amount'].replace('\n', ' ').replace(' ',''))
            print(val['regional']['japan-east'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多