How do I find elements by class name without repeating the output? I have two class to scrape hdrlnk
and results-price
. I wrote the code like this:
x = driver.find_elements_by_class_name(['hdrlnk','result-price'])
and it gives me some error. I have another code that I tried and here it is:
x = driver.find_elements_by_class_name('hdrlnk'),
y = driver.find_elements_by_class_name('result-price')
for xs in x:
for ys in y:
print(xs.text + ys.text)
But I got the result like this
sony 5 disc cd changer$40
sony 5 disc cd changer$70
sony 5 disc cd changer$70
sony 5 disc cd changer$190
sony 5 disc cd changer$190
sony 5 disc cd changer$190
sony 5 disc cd changer$190
sony 5 disc cd changer$10
The part of the HTML structure that I am trying to scrape
<p class="result-info">
<span class="icon icon-star" role="button" title="save this post in your favorites list">
<span class="screen-reader-text">favorite this post</span>
</span>
<time class="result-date" datetime="2019-11-07 18:20" title="Thu 07 Nov 06:20:56 PM">Nov 7</time>
<a href="https://vancouver.craigslist.org/rch/ele/d/chandeliers/7015824686.html" data-id="7015824686" class="result-title hdrlnk">CHANDELIERS</a>
<span class="result-meta">
<span class="result-price">$800</span>
<span class="result-hood"> (Richmond)</span>
<span class="result-tags">
<span class="pictag">pic</span>
</span>
<span class="banish icon icon-trash" role="button">
<span class="screen-reader-text">hide this posting</span>
</span>
<span class="unbanish icon icon-trash red" role="button" aria-hidden="true"></span>
<a href="#" class="restore-link">
<span class="restore-narrow-text">restore</span>
<span class="restore-wide-text">restore this posting</span>
</a>
</span>
</p>
The first element is repeated but I got the correct value for the second one. How do I correct this error?
` elements? And each of them contains a `hdrlnk` and a `result-price`?
– Code-Apprentice Nov 08 '19 at 05:11