2017-04-03 25 views
1

如果單獨測試,下面定義的xpath正常工作。但是,當我從存儲對象中調用 並使該結構看起來像下面那樣時,出現問題併產生無序結果。如果有的話,請忽略我的語言錯誤。需要一點點推動才能構建Xpath表達式

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()') 

我的問題是:如何如果是從「存儲」拍攝,並內置「姓名」,「地址」和「電話」 當我試着上面做構建XPath表達式。謝謝。

如果需要,以下是該表達式的html元素。

<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> 
+0

可能與[在XPath中//和.//有什麼區別?](http://stackoverflow.com/questions/39902960/what-is-the-difference-between在-xpath) – kjhughes

回答

1

如果你想獲得子/後代元素已經定義item,你需要使用.//電流("item")元素點,但不是//root元素上的點。請嘗試以下操作:

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()') 
+0

謝謝先生安德森,爲您的尖銳反應和有效的解決方案。它像魔術一樣工作。再次感謝。 – SIM