Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从我需要的文本中跳过或截断字符或符号。用靓汤刮网_Python_Python 3.x_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 如何从我需要的文本中跳过或截断字符或符号。用靓汤刮网

Python 如何从我需要的文本中跳过或截断字符或符号。用靓汤刮网,python,python-3.x,web-scraping,beautifulsoup,Python,Python 3.x,Web Scraping,Beautifulsoup,我需要在divtag之间获得price(61990),但如何去除货币符号 和这里一样,我只需要获取评级(4.7),但之后我不需要任何东西,比如imgsrc。我怎么能忽视它?还是跳过它 代码示例: from bs4 import BeautifulSoup import requests price = [] ratings=[] response = requests.get("https://www.flipkart.com/laptops/~buyback-guarantee-on-l

我需要在
div
tag之间获得price(61990),但如何去除货币符号

和这里一样,我只需要获取评级(4.7),但之后我不需要任何东西,比如
imgsrc
。我怎么能忽视它?还是跳过它

代码示例:

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'})

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

从bs4导入美化组
导入请求
价格=[]
评级=[]
响应=请求。获取(“https://www.flipkart.com/laptops/~z笔记本电脑回购担保-/pr?sid=6bo%2Cb5g&uniq”)
soup=BeautifulSoup(response.text'html.parser')
对于汤中的a.findAll('a',href=True,attrs={'class':''u 31qSD5'}):
price=a.find('div',attrs={'class':''u 1vC4OE_2rQ-NK'})
rating=a.find('div',attrs={'class':'hGSR34'})
印刷品(价格)
印刷品(评级)

伟大的非常感谢。
Out[110]: '4.3'
Out[111]: '52,990'