如何从我需要的文本中跳过或截断字符或符号。网上抓漂亮的汤

我需要获得div标签之间的价格(61,990),但是如何摆脱货币符号?

如何从我需要的文本中跳过或截断字符或符号。网上抓漂亮的汤

与此处相同,我只需要获得评分(4.7),但此后我就不需要任何内容​​,例如img src。我怎么能忽略它?还是跳过它?

如何从我需要的文本中跳过或截断字符或符号。网上抓漂亮的汤

代码示例:

from bs4 import BeautifulSoup
import requests

price = []
ratings=[]
response = requests.get("https://www.flipkart.com/laptops/~buyback-guarantee-on-laptops-/pr?sid=6bo%2Cb5g&uniq")
soup = BeautifulSoup(response.text,'html.parser')
for a in soup.findAll('a',href=True,attrs={'class':'_31qSD5'}): 
    price=a.find('div',attrs={'class':'_1vC4OE _2rQ-NK'})
    rating=a.find('div',attrs={'class':'hGSR34'})
daiqilin1970 回答:如何从我需要的文本中跳过或截断字符或符号。网上抓漂亮的汤

这里。您只需要使用.text方法并将其视为普通字符串即可。在这种情况下,请保留除第一个字符外的所有字符。

from bs4 import BeautifulSoup
import requests

price = []
ratings=[]
response = requests.get("https://www.flipkart.com/laptops/~buyback-guarantee-on-laptops-/pr?sid=6bo%2Cb5g&uniq")
soup = BeautifulSoup(response.text,'html.parser')
for a in soup.findAll('a',href=True,attrs={'class':'_31qSD5'}):
    price=a.find('div',attrs={'class':'_1vC4OE _2rQ-NK'}).text[1:]
    rating=a.find('div',attrs={'class':'hGSR34'}).text
print(price)
print(rating)
Out[110]: '4.3'
Out[111]: '52,990'
本文链接:https://www.f2er.com/3143621.html

大家都在问