删除字符串末尾的字符

时间:2020-09-29 12:08:44

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

我正在使用网页抓取功能显示来自newegg的图形卡价格。在我抓取的某些文本上,在价格也被抓取之后,还有不需要的文本。仅显示价格文本的最有效方法是什么。

price_container = container.findAll("li", {"class": "price-current"})
price = price_container[0].text
if len(price) > 7:
  

价格(我要保留的位)的长度永远不会超过7个字符,因此我认为我可以使用此if语句删除不需要的文本,但是我不确定如何这样做,因为每个价格在其后都有不同长度的不需要的文本。 / p>

3 个答案:

答案 0 :(得分:1)

您可以使用正则表达式。

或者选择一个字符串并从中提取数字。 示例:

[float(p) for p in price.split() if p.isdigit()] # Will give you an array of the numbers in the string. You can then join them back together.

也许不是您想要的东西,但希望能对您有所帮助:)

答案 1 :(得分:1)

使用正则表达式:

I am trying to refactor the package name

答案 2 :(得分:0)

if len(price) > 7:
    price = price[:-1] #This will reasign the string/list to a string/list with all the characters except for the last one. 
相关问题