【问题标题】:try to get the first attributes from the 1st store in python 3尝试从 python 3 中的第一个商店获取第一个属性
【发布时间】:2019-02-25 17:43:17
【问题描述】:

我正在尝试从特定页面获取第一个商店属性, 直到现在,我都无法到达它,因为我认为它是一种商店阵列 - 我怎么才能只得到第一个?

import urllib.request
import requests
from bs4 import BeautifulSoup

with urllib.request.urlopen('https://www.zap.co.il/') as response:
html = response.read()
check ='https://www.zap.co.il/search.aspx?keyword='+'20KN0061IV'
r = requests.get(check)
html = requests.get(r.url)
bsObj = BeautifulSoup(html.content,'xml')
price = bsObj.find('div',attrs={'class':'StoresLines'})
print(price)

我得到了所有的商店 - 但我只想要第一家商店 - 怎么做?

【问题讨论】:

  • 第一个请求有什么意义?是健康检查吗?
  • 我需要'StoresLines'类中关于第一家商店的所有数据。链接是[链接]zap.co.il/model.aspx?modelid=976556

标签: python python-3.x beautifulsoup attributes


【解决方案1】:

要访问顶级商店(休息前的商店)可以替换

price = bsObj.find('div',attrs={'class':'StoresLines'})

与:

price = bsObj.select_one('div.StoreLine')

如果您想从 break 下方的一堆行中获取 top store,请将同一行替换为:

price = bsObj.select_one('div.StoresLines div.StoreLine')

希望这会有所帮助!

最后一条建议的输出:

<div class="StoreLine" data-distance="0" data-hit="-1240851152" data-iseilat="False" data-price-caption="מחיר סופי" data-prod-price="2609" data-product-id="688189532" data-siteid="1245" data-sog="c-pclaptop" data-store-line="" data-supply="2" data-total-price="2609" data-total-rank="4.48" pid="688189532">
<div class="StoreLine">
<div class="StoreLogo">
<a aria-label="לקריאת חוות דעת על שניידר" href="/clientcard.aspx?siteid=1245" id=""><img alt="שניידר" border="0" class="Logo" id="" src="https://img.zap.co.il/pics/imgs/nsite/newui/newssite-1245.gif"/></a>
</div>
<div class="StoreReviews">
<div class="BlueStars">
<span style="width: 99px;"></span>
</div>
<div class="StoreReviewsTxt">
<a aria-label="לקריאת 67 חוות דעת על Lenovo E480 20KN0061IV יבואן רשמי במלאי 1134 בסך הכל" class="LongLine" href="/clientcard.aspx?siteid=1245">67 חוות דעת בשנה האחרונה<br/>1134 בסך הכל</a>
</div>
<div class="StoreTags">
<div class="MapContainer1245 Regular" style="display: none;"></div>
</div>
</div>
<div class="StoreInfo">
<div class="StoreTitle">
<div class="StoreName">
<div aria-label="למידע נוסף על Lenovo E480 20KN0061IV יבואן רשמי במלאי" class="InfoBtn" onclick="javascript:ShowStoreTooltip(this, 'Lenovo E480 20KN0061IV יבואן רשמי במלאי','מוצר חדש 20KN0061IV יצרן: Lenovo , סוג מוצר: מחשב נייד , דגם מעבד: Intel® Core™ i5-8250U Processor (6M Cache, up to 3.40 GHz), נפח זכרון פנימי: 8GB , נפח אחסון: 256GB , סוג דיסק: PCIe NVMe M.2 SSD , גודל מסך: 14.0 , רזולוציה: 1920x1080 , דגם כרטיס מסך:');"></div>
<a aria-label="לקנייה ב-שניידר Lenovo E480 20KN0061IV יבואן רשמי במלאי" class="hl_ProductTitle modelClick" href="/fs.aspx?pid=688189532&amp;sog=c-pclaptop" id="" rel="nofollow" target="_blank">Lenovo E480 20KN0061IV יבואן רשמי במלאי</a>
</div>
</div>
<div class="ProdInfo">
<div class="infoItem">
<span class="label">מחיר:</span>
                        2,609 ₪
                    </div>
<div class="infoItem">
<span class="label">
                                משלוח:</span>
                            חינם
                        </div>
<div class="infoItem">
<span class="label">זמן אספקה:</span>
                            יומיים
                        </div>
</div>
</div>
<div class="FinalPrice">
<div class="FinalPriceLabel">מחיר סופי:</div>
<div class="PriceNum">2,609 ₪ </div>
</div>
<div class="BuyButtons">
<a aria-label="לקנייה ב-שניידר Lenovo E480 20KN0061IV יבואן רשמי במלאי" href="/fs.aspx?pid=688189532&amp;sog=c-pclaptop" id="" rel="nofollow" target="_blank"><div class="BuyBtn orange-style modelClick" rel="nofollow">
<div class="BuyBtnText">
                            לקנייה
                        </div>
</div></a>
<div class="BuyButtonsTxt">
                ב-<a aria-label="לקנייה ב-שניידר Lenovo E480 20KN0061IV יבואן רשמי במלאי" href="/fs.aspx?pid=688189532&amp;sog=c-pclaptop" id="" target="_blank">שניידר</a>
</div>
</div>
</div>
</div>

在哪里:

price.select_one('.FinalPrice .PriceNum').text

返回:

'2,609 ₪ '

【讨论】:

  • 谢谢,但我要买所有的商店。我只需要“StoresLines”类中的第一家商店。链接是[链接]zap.co.il/model.aspx?modelid=976556
  • 你确定bsObj.select_one('div.StoresLines div.StoreLine') 没有返回你想要的吗?
猜你喜欢
  • 2016-11-03
  • 2010-11-10
  • 2022-11-11
  • 1970-01-01
  • 2013-06-21
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
相关资源
最近更新 更多