我目前正在抓取该网站:
https://listado.mercadolibre.com.ve/oficinas-suministros-papeleria-papel-en-aragua/resma-carta-alpes
在将“文本”属性应用于十进制价格变量之前,我一直是成功的。
这是我一直在使用的代码:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://listado.mercadolibre.com.ve/oficinas-suministros-papeleria-papel-en-aragua/resma-carta-alpes'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
containers = page_soup.findAll("div", {"class": "item__info-container"})
for container in containers:
title_container = container.a.text.strip()
price_container_fraction = container.find("span" ,{"class":"price__fraction"}).text
price_container_decimal = container.find("span" ,{"class":"price__decimals"})
print(title_container)
print(price_container_fraction)
print(price_container_decimal)
这是html广告块:
<div class="item__info-container ">
<div class="item__info item--hide-right-col ">
<h2 class="item__title list-view-item-title"> <a class="item__info-title" href="https://articulo.mercadolibre.com.ve/MLV-523616759-resma-tamano-carta-marca-alpes-caja-_JM"> <span class="main-title"> Resma Tamaño Carta Marca Alpes Caja </span> </a></h2>
<div class="price__container">
<div class="item__price "> <span class="price__symbol">Bs.</span> <span class="price__fraction">4</span> <span class="price__decimals">50</span></div>
</div>
<div class="item__stack_column">
<div class="item__stack_column__info item__only-status">
<div class="stack_column_item status">
<div class="item__status">
<div class="item__condition"> 2 vendidos </div>
</div>
</div>
</div>
</div>
<div class="stack_colum_right without-attributes without-reviews">
<div class="stack_column_right__bottom "> </div>
</div>
</div>
</div>
这是结果:
我需要收集价格的2个元素,然后添加它们,但是没有运气。有帮助吗?
答案 0 :(得分:1)
price_container_decimal打印整个html内容,添加.text仅打印十进制值。
price_container_decimal = container.find("span" ,{"class":"price__decimals"}).text
答案 1 :(得分:1)
问题似乎是price_decimals
范围未总是填充,find
返回None
。我已经将以下内容添加到您的代码中并对其进行了测试,它产生了我猜想的是您所需的输出。如果条件条件检查None
是否为True,它将price_container_decimal
设置为00
,如果不是,则将其设置为跨度的text属性值。
price_container_decimal = container.find("span" ,{"class":"price__decimals"})
if price_container_decimal == None:
price_container_decimal = "00"
else:
price_container_decimal = container.find("span" ,{"class":"price__decimals"}).text