我怎样才能拆分价格以获得一个价格

How can I split the price to get one price

如何将输出分成两部分并只得到第一部分或第二部分我试过这个all_original_price = [o.text.split('>').split('₹') for o in all_original_price] but it did not work

from bs4 import BeautifulSoup as soup
import pandas as pd
import re 

url = "https://www.amazon.in/s?k=smart+watch&page=1"

original_price =[]

amazon_data = urlopen(url)
amazon_html = amazon_data.read()
a_soup = soup(amazon_html,'html.parser')
all_original_price = a_soup.findAll('span',{'class':'a-price a-text-price'})
all_original_price = [o.text.split('>') for o in all_original_price]
for item in all_original_price:
    original_price.append(item)
print(original_price)```


OUTPUT
[['₹4,999₹4,999'], ['₹6,400₹6,400'], ['₹4,999₹4,999'], ['₹5,999₹5,999'], ['₹4,999₹4,999'], ['₹5,999₹5,999'], ['₹3,999₹3,999'], ['₹6,990₹6,990'], ['₹7,999₹7,999'], ['₹1,599₹1,599'], ['₹5,999₹5,999'], ['₹4,999₹4,999'], ['₹5,999₹5,999'], ['₹4,999₹4,999'], ['₹4,999₹4,999'], ['₹9,999₹9,999'], ['₹6,999₹6,999']]

试试这个:

all_original_price = [o.text.split('>')[0].split('₹')[1:] for o in all_original_price]

尝试:

url = "https://www.amazon.in/s?k=smart+watch&page=1"

original_price =[]

amazon_data = urlopen(url)
amazon_html = amazon_data.read()
a_soup = soup(amazon_html,'html.parser')
all_original_price = a_soup.findAll('span',{'class':'a-price a-text-price'})
all_original_price = [o.find('span', {'class': 'a-offscreen'}).text.split('>') for o in all_original_price]
for item in all_original_price:
    original_price.append(item[0])

输出:

>>> original_price
['₹4,999',
 '₹6,400',
 '₹4,999',
 '₹5,999',
 '₹4,999',
 '₹5,999',
 '₹3,999',
 '₹6,990',
 '₹7,999',
 '₹1,599',
 '₹5,999',
 '₹4,999',
 '₹5,999',
 '₹4,999',
 '₹4,999',
 '₹9,999',
 '₹6,990']

您可以简单地使用 re 模块。由于您已经导入了它,所以我不会在下面的代码中显示导入行:

[re.search(r"₹\d+(,\d+)?", x[0]).group() for x in all_original_price]

输出

['₹4,999',
 '₹6,400',
 '₹4,999',
 '₹5,999',
 '₹4,999',
 '₹5,999',
 '₹3,999',
 '₹6,990',
 '₹7,999',
 '₹1,599',
 '₹5,999',
 '₹4,999',
 '₹5,999',
 '₹4,999',
 '₹4,999',
 '₹9,999',
 '₹6,999']