SIM
SIM

Reputation: 22440

Need a little push to build an Xpath expression

The xpath I have defined below is working properly if tested individually. However, when I call it from storage object and make that structure look like as underneath, trouble comes up and generates disorganized results. Ignore my linguistic mistakes, if any.

Storage=xpath('//div[@class="info"]')
for item in Storage:
    Name=item.xpath('//span[@itemprop="name"]/text()')
    Address=item.xpath('//span[@itemprop="streetAddress" and @class="street-address"]/text()')
    Phone=item.xpath('//div[@itemprop="telephone" and @class="phones phone primary"]/text()')

My question is: How to build an xpath expression If it is taken from "storage" and built "Name", "Address", and "Phone" as I tried to do above. Thanks.

Here is the html element for that expression, if needed.

<div class="info"><h2 class="n">36.&nbsp;<a href="/los-angeles-ca/mip/the-coffee-table-eagle-rock-11287432?lid=11287432" data-analytics="{&quot;target&quot;:&quot;name&quot;,&quot;feature_click&quot;:&quot;&quot;}" rel="" class="business-name" data-impressed="1"><span itemprop="name">The Coffee Table Eagle Rock</span></a></h2><div data-tripadvisor="{&quot;rating&quot;:&quot;4.0&quot;,&quot;count&quot;:&quot;11&quot;}" data-israteable="true" class="info-section info-primary"><a href="/los-angeles-ca/mip/the-coffee-table-eagle-rock-11287432?lid=11287432#yp-rating" data-analytics="{&quot;click_id&quot;:22,&quot;listing_features&quot;:&quot;ratings&quot;}" class="rating hasExtraRating" data-impressed="1"><div class="result-rating three half "><span class="count">(5)</span></div></a><a href="/los-angeles-ca/mip/the-coffee-table-eagle-rock-11287432?lid=11287432#ta-rating" data-analytics="{&quot;click_id&quot;:2396}" class="ta-rating-wrapper" data-impressed="1"><div class="ta-rating extra-rating ta-4-0"></div><span class="ta-count">(11)</span></a><p itemscope="" itemtype="http://schema.org/PostalAddress" itemprop="address" class="adr"><span itemprop="streetAddress" class="street-address">1958 Colorado Blvd</span><span itemprop="addressLocality" class="locality">Los Angeles,&nbsp;</span><span itemprop="addressRegion">CA</span>&nbsp;<span itemprop="postalCode">90041</span></p><div itemprop="telephone" class="phones phone primary">(323) 255-2200</div></div><div class="info-section info-secondary"><div class="categories"><a href="/los-angeles-ca/coffee-espresso-restaurants" data-analytics="{&quot;click_id&quot;:1171,&quot;adclick&quot;:false,&quot;listing_features&quot;:&quot;category&quot;,&quot;events&quot;:&quot;&quot;}" data-impressed="1">Coffee &amp; Espresso Restaurants</a><a href="/los-angeles-ca/bars" data-analytics="{&quot;click_id&quot;:1171,&quot;adclick&quot;:false,&quot;listing_features&quot;:&quot;category&quot;,&quot;events&quot;:&quot;&quot;}" data-impressed="1">Bars</a></div><div class="links"><a href="http://www.coffeetablelounge.com" rel="nofollow" target="_blank" data-analytics="{&quot;click_id&quot;:6,&quot;act&quot;:2,&quot;dku&quot;:&quot;http://www.coffeetablelounge.com&quot;,&quot;FL&quot;:&quot;url&quot;,&quot;target&quot;:&quot;website&quot;,&quot;LOC&quot;:&quot;http://www.coffeetablelounge.com&quot;,&quot;adclick&quot;:true}" class="track-visit-website" data-impressed="1">Website</a><a href="/listings/11287432/menu?lid=11287432" data-analytics="{&quot;click_id&quot;:1614,&quot;target&quot;:&quot;menus&quot;,&quot;listing_features&quot;:&quot;menu-link&quot;}" class="menu" data-impressed="1">Menu</a></div><a data-analytics="{&quot;adclick&quot;:true,&quot;events&quot;:&quot;event7,event6&quot;,&quot;category&quot;:&quot;8004238&quot;,&quot;impression_id&quot;:&quot;fbd98612-6b8a-43c2-b31e-fd579de20126&quot;,&quot;listing_id&quot;:&quot;11287432&quot;,&quot;item_id&quot;:-1,&quot;listing_type&quot;:&quot;free&quot;,&quot;ypid&quot;:&quot;11287432&quot;,&quot;content_provider&quot;:&quot;MDM&quot;,&quot;srid&quot;:&quot;L-webyp-1c6db222-cc63-48d8-90d1-2d5dc8754cca-11287432&quot;,&quot;item_type&quot;:&quot;PUP&quot;,&quot;lhc&quot;:&quot;8004238&quot;,&quot;ldir&quot;:&quot;LA&quot;,&quot;rate&quot;:3.5,&quot;hasTripAdvisor&quot;:true,&quot;mip_claimed_staus&quot;:&quot;mip_unclaimed&quot;,&quot;mip_ypid&quot;:&quot;11287432&quot;,&quot;click_id&quot;:523,&quot;listing_features&quot;:&quot;orderonline&quot;}" href="https://yellowpages.pingup.com/Bkm3xG?ypid=11287432&amp;uvid=t3pfPllxtLYkH2dlkSbiCC1marvZprsz1YhqhycO80NYrDv0OMX3uTJ3ryFG464RywmpWCrB&amp;source=web-prod" rel="nofollow" target="_blank" class="action order-online" data-impressed="1">Order Online</a></div><div class="preferred-listing-features"></div><div class="snippet"><figure class="avatar-1 color-1"></figure><p class="body with-avatar">I went here recently with my 2 year old for breakfast. I got the Silverlake omelet and the breakfast sandwich for my son. The food was great (especi…</p></div></div>

Upvotes: 1

Views: 72

Answers (1)

Andersson
Andersson

Reputation: 52675

If you want to get child/descendant elements of already defined item, you need to use .// to point on current ("item") element, but not // that points on root element. Try below:

Storage=xpath('//div[@class="info"]')
for item in Storage:
    Name=item.xpath('.//span[@itemprop="name"]/text()')
    Address=item.xpath('.//span[@itemprop="streetAddress" and @class="street-address"]/text()')
    Phone=item.xpath('.//div[@itemprop="telephone" and @class="phones phone primary"]/text()')

Upvotes: 1

Related Questions