【问题标题】:Beautiful Soup web scraping and working with integersBeautiful Soup 网页抓取和整数处理
【发布时间】:2020-10-24 03:27:57
【问题描述】:

我有以下代码,使用 BeautifulSoup 和 Python 对一些冠状病毒统计数据进行网络抓取(并随后计算出一个百分比):

url = "https://www.worldometers.info/coronavirus/"
req = requests.get(url)
bsObj = BeautifulSoup(req.text, "html.parser")
data = bsObj.find_all("div",class_ = "maincounter-number")
    
totalcases=data[0].text.strip()
recovered=data[2].text.strip()
print(totalcases+3)

percentagerecovered=recovered/totalcases*100

我遇到的问题是为已恢复的变量百分比生成所需的值。

我想使用整数,但上面的方法不起作用,所以我尝试了:

percentagecovered=int(recovered)/int(totalcases)*100 but it gave this error:

  File "E:\webscraper\webscraper\webscraper.py", line 17, in <module>
    percentagerecovered=int(recovered)/int(totalcases)*100
ValueError: invalid literal for int() with base 10: '6,175,537'

但是,当我删除铸件并尝试仅打印以查看它给出了不同错误的值时,我很难理解。

我改成:

totalcases=data[0].text.strip()
recovered=data[2].text.strip()
print(totalcases+3)

percentagerecovered=recovered/totalcases*100

错误

File "webscraper.py", line 16, in <module>
    print(totalcases+3)
TypeError: can only concatenate str (not "int") to str

我只是想使用 split 方法获取这些字符串,然后假设它们是整数来处理它们。

目前,当我传递它们(不进行转换)时,它不会在页面上显示任何内容......但是当我将它们转换为 int 时,我会收到错误。我做错了什么?

我也试过了:

totalcases=int(totalcases)
recovered=int(recovered)

但这又产生了一个错误:

File "webscraper.py", line 17, in <module>
    totalcases=int(totalcases)
ValueError: invalid literal for int() with base 10: '11,018,642'

我也试过这个:(去掉逗号)如下 cmets 中的建议:

totalcases=data[0].text.strip()
recovered=data[2].text.strip()
totalcases=totalcases.strip(",")
totalcases=int(totalcases)
recovered=recovered.strip(",")
recovered=int(recovered)
percentagerecovered=recovered/totalcases*100

错误:

totalcases=int(totalcases) ValueError: int() 以 10 为基数的无效文字:'11,018,684'

我注意到下面的函数(我还没有尝试过)之类的解决方案,但对于我正在尝试做的事情来说,它们似乎不必要地复杂。什么是最好、最简单/最优雅的解决方案。

这似乎是正确的,但仍然会产生错误:

int(totalcases.replace(',', ''))
int(recovered.replace(',', ''))

错误:

File "webscraper.py", line 25, in <module>
    percentagerecovered=recovered/totalcases*100
TypeError: unsupported operand type(s) for /: 'str' and 'str'

【问题讨论】:

  • 好的 - 感谢您的观察。但是我该如何将它作为一个整数来处理呢?
  • 您可以去掉逗号,然后尝试转换为整数,int("11,018,642".replace(",",""))
  • 我显然不能使用这个数字,因为它是动态生成的,并保存在变量 totalcases 和恢复中......所以我需要剥离,不知道确切的格式。
  • 此外,如果我尝试 totalcases=int(totalcases.replace(",","")) 会导致错误:recovered=int(totalcases.replace(",","" )) AttributeError: 'int' 对象没有属性 'replace'
  • 根据您的建议更新了上面的问题。

标签: python types beautifulsoup casting


【解决方案1】:

我写了这个返回给你一个数字的小函数,所以你可以增加它或者做任何你想做的事情

def str_to_int(text=None):
    if text == None:
        print('no text')
    else:
        text = text.split(',')
        num = int(''.join(text))
        return num

例如,您的总病例数:“11,018,642”,因此您可以这样做:

totalcases = str_to_int('11,018,642')

现在你可以用它做totalcases*100 或其他任何事情

【讨论】:

  • 会试试这个 - 但我真的在寻找,为了教学目的,最简单的方法来做到这一点。为什么 int(totalcases.replace(',', '')) int(recovered.replace(',', '')) 不起作用?
【解决方案2】:

另一种简单的方法:

totalcases= int(data[0].text.strip().replace(',',''))
recovered = int(data[2].text.strip().replace(',',''))

【讨论】:

  • 你是个明星——我认为是这样。但是为什么我的方法,看起来很相似,只是在一个额外的行上,工作(见问题的最后一部分更新)
  • @MissComputing 从编辑中不清楚究竟是哪个版本的totalcases 导致了最后一个错误(你有几个问题)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
  • 1970-01-01
  • 2016-05-16
  • 2017-07-29
  • 1970-01-01
相关资源
最近更新 更多