【发布时间】:2021-12-20 04:12:47
【问题描述】:
我正在做一个项目,用 selenium、python 导航和抓取网站。
hemnet_path = "https://www.hemnet.se/"
driver = webdriver.Chrome(path, chrome_options=options)
driver.get(hemnet_path)
脚本导航到this site 并循环访问所有列表以通过加载如下结果元素来获取价格、位置、超链接等:
result = driver.find_elements(By.CLASS_NAME,"sold-property-listing")
然后它会读取如下信息:
for r in result:
try:
location = r.find_element(By.CLASS_NAME,"sold-property-listing__location")
except:
location = 'Missing'
我遇到的问题是超链接字符串(屏幕截图)不像其他元素那样“表现”。
我似乎无法按类或 CSS 选择器和 href 进行选择,因为我收到以下错误:
但问题是,当我遍历结果时,XPATH 总是引用同一个广告,在本例中为 2。我考虑过在 XPATH 中循环整数,但结果证明这是非常不可靠的。
我不确定我在这里遗漏了什么以及为什么会收到这个奇怪的错误。
对此的任何帮助将不胜感激。 下面提供了 ... 元素的 HTML。
<li class="sold-results__normal-hit">
<a class="hcl-card" data-target-blank="true" href="https://www.hemnet.se/salda/lagenhet-3rum-barkarbystaden-jarfalla-kommun-karlslundsvagen-26-1492088" rel="noopener" target="_blank">
<div class="sold-property-listing">
<div class="sold-property-listing__location">
<h2 class="sold-property-listing__heading">
<span class="property-icon property-icon--result"><svg width="14" height="16" viewBox="0 0 14 16" xmlns="http://www.w3.org/2000/svg"><title>Lägenhet</title><desc><span class="svg-icon__fallback-text">Lägenhet</span></desc><path class="svg-icon__shape" d="M0 1.333v13.334C0 15.403.597 16 1.333 16h4v-5.333H8V16h4c.737 0 1.333-.597 1.333-1.333V1.333C13.333.597 12.737 0 12 0H1.333C.597 0 0 .597 0 1.333" fill="#C1569D" fill-rule="evenodd"></path></svg>
</span>
<span class="item-result-meta-attribute-is-bold item-link qa-selling-price-title">Karlslundsvägen 26</span>
</h2>
<div>
<span class="hide-element">
Lägenhet
</span>
<span class="item-link">
Barkarbystaden,
</span> Järfälla kommun
</div>
</div>
<div class="sold-property-listing__size">
<div class="clear-children">
<div class="sold-property-listing__subheading sold-property-listing--left">
78 m²
3 rum
</div>
<div class="sold-property-listing__fee">
4 233 kr/mån
</div>
</div>
</div>
<div class="sold-property-listing__price">
<div class="clear-children">
<span class="sold-property-listing__subheading sold-property-listing--left">
Slutpris 3 295 000 kr
</span> </div>
<div class="clear-children">
<div class="sold-property-listing__sold-date sold-property-listing--left">
Såld 5 november 2021
</div>
<div class="sold-property-listing__price-per-m2 sold-property-listing--left">
42 244 kr/m²
</div>
</div>
</div>
<div class="sold-property-listing__price-change">
±0 %
</div>
<div class="sold-property-listing__broker">
Länsförsäkringar Fastighetsförmedling Järfälla
</div>
<div class="sold-property-listing__labels">
<div class="hcl-labels-list hcl-labels-list--row-direction">
<span class="hcl-labels-list__label-item">
<span class="hcl-label hcl-label--feature hcl-label--on-white-background">
Uteplats
</span>
</span>
<span class="hcl-labels-list__label-item">
<span class="hcl-label hcl-label--feature hcl-label--on-white-background">
Hiss
</span>
</span>
</div>
</div>
</div>
</a>
</li>
【问题讨论】: