2017-04-03 62 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