简体   繁体   中英

Extracting full URL from href tag in scrapy

I'm trying to use scrapy to scrape URLs from offers from this site

This is the code I tried:

url = response.css('a[data-tracking="click_body"]::attr(href)').extract()

But my code returns something very different from a URL. Here is the HTML code of the div I'm interested in.

<div class="offer-item-details">
                <header class="offer-item-header">
            <h3>
                <a href="https://www.otodom.pl/oferta/gdansk-pod-inwestycje-cicha-lokalizacja-ID46DXu.html#ab04badaa0" data-tracking="click_body" data-tracking-data="{&quot;touch_point_button&quot;:&quot;title&quot;}" data-featured-name="promo_top_ads">
                    <strong class="visible-xs-block">42 m²</strong>
                    <span class="text-nowrap">
                        <span class="offer-item-title">Gdańsk/ Pod Inwestycje/ Cicha Lokalizacja</span>
                    </span>
                </a>
            </h3>
            <p class="text-nowrap"><span class="hidden-xs">Mieszkanie na sprzedaż: </span>Gdańsk, Ujeścisko-Łostowice, Łostowice</p>
                        <div class="vas-list-no-offer">
                <a class="button-observed observe-link favourites-button observed-text svg-heart add-to-favourites" data-statkey="ad.observed.list" rel="nofollow" data-id="60688916" href="#" title="Obserwuj">
                    <div class="observed-text-container" style="display: flex;">

                        <span class="icon observed-60688916"></span>
                        <i class="icon-heart-filled"></i>
                        <div class="observed-label">Dodaj do ulubionych</div>
                    </div>
                </a>
            </div>
        </header>
        <ul class="params
            " data-tracking="click_body" data-tracking-data="{&quot;touch_point_button&quot;:&quot;body&quot;}">
                            <li class="offer-item-rooms hidden-xs">2 pokoje</li>
                                                            <li class="offer-item-price">
                                                                                    346 000 zł                                                                                                                                </li>
                                                                                                                                <li class="hidden-xs offer-item-area">42 m²</li>
                                                                                                                                                                        <li class="hidden-xs offer-item-price-per-m">8 238 zł/m²</li>
                                                                                                                                                                                                                                    </ul>
            </div>

Copied selector of that tag:

#offer-item-ad_id45Wog > div.offer-item-details > header > h3 > a

Copied xPath

//*[@id="offer-item-ad_id45Wog"]/div[1]/header/h3/a

Copied full xPath

/html/body/div[3]/main/section[2]/div/div/div[1]/div/article[1]/div[1]/header/h3/a

Your code gives you a list of the URLs. The extract() method in this case gets a list. To allow scrapy to extract the data you will have to do a for loop and yield statement.

url = response.css('a[data-tracking="click_body"]::attr(href)').extract()
for a in url: 
    yield{'url', a}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM