如何解决美丽的汤,get_text但不是<span>文本
给出以下标记: [MARKUP] [1]
我需要在列中获取数字 182 ,在另一列中获取 58 。我已经具有跨度,但是当我调用 div.get_tex()或字符串时,它返回= 18258 (两个数字)
这是我的代码:
prices= soup.find_all('div',class_='grilla-producto-precio')
cents= []
price= []
for px in prices:
### here i need to get the number 182 and append it to "price"
for spn in px.find('span'):
cents.append(spn)
如何在没有SPAN的情况下单独获得价格182?谢谢!!!! [1]:https://i.stack.imgur.com/ld9qo.png
解决方法
您问题的答案与this question的答案几乎相同。
import turtle
import random
wn = turtle.Screen()
wn.bgcolor('lightblue')
svea = turtle.Turtle()
sample=[1,2,3]
click_count=0
def square(t,size,r,g,b,x,y,z,click_count):
if click_count==3:
quit()
else:
t.pencolor(x,z)
t.begin_fill()
for i in range(4):
t.forward(size)
t.right(90)
t.fillcolor(r,b)
t.end_fill()
def triangle(t,z):
t.pencolor(x,z)
t.begin_fill()
for i in range(3): # repeat four times
t.forward(size)
t.left(120)
t.fillcolor(r,b)
t.end_fill()
def rectangle(t,z)
t.begin_fill()
for i in range(2): # repeat four times
t.forward(size)
t.left(90)
t.forward(size*1.25)
t.left(90)
t.fillcolor(r,b)
t.end_fill()
def drawOnClick(x,y):
wn.onclick(None)
size=random.randrange(50,150)
svea.up()
svea.goto(x,y)
svea.down()
square(svea,random.randrange(0,255),random.randint(0,3))
triangle(svea,255))
svea.up()
svea.goto(x+(size*0.4),y-size)
svea.down()
rectangle(svea,size/4,255))
svea.up()
svea.goto(x+(size*0.15),y-(size*0.15))
svea.down()
square(svea,size/5,2)
svea.up()
svea.goto(x+(size*0.65),2)
wn.onclick(drawOnClick)
#click_count+=1
#if click_count==3:
#quit()
def drawOnClick2(x,y):
size=random.randrange(10,20)
svea.begin_fill()
svea.up()
svea.goto(x,y)
svea.down()
svea.circle(size)
svea.right(90)
svea.forward(size*2)
svea.right(45)
svea.forward(size*2)
svea.right(180)
svea.forward(size*2)
svea.right(90)
svea.forward(size*2)
svea.right(180)
svea.forward(size*2)
svea.right(45)
svea.forward(size)
svea.right(90)
svea.forward(size*1.5)
svea.right(180)
svea.forward(size*3)
svea.right(180)
#wn.onclick(drawOnClick2)
wn.onclick(drawOnClick)
#wn.onclick(None)
# wn.exitonclick()
输出:
from bs4 import BeautifulSoup
html = """
<div class = "grilla-producto-precio">
" $"
"182"
<span>58</span>
</div>
"""
soup = BeautifulSoup(html,'html5lib')
prices = soup.find_all('div',class_ = "grilla-producto-precio")
cents = []
for px in prices:
txt = px.find_next(text=True).strip()
txt = txt.replace('"','')
txt = int(txt.split("\n")[-1])
cents.append(txt)
,
另一种解决方案是检查字符串isdigit():
from bs4 import BeautifulSoup
txt = """
<div class = "grilla-producto-precio">
" $"
"182"
<span>58</span>
</div>
"""
soup = BeautifulSoup(txt,"html.parser")
data = soup.find("div",class_="grilla-producto-precio").next
price = [int("".join(d for d in data if d.isdigit()))]
print(price) # Output: [182]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。