如果单独测试,我在下面定义的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表达式如果它取自" storage"并建立"姓名","地址"和"电话" 正如我上面尝试的那样。感谢。
如果需要,这是该表达式的html元素。
<div class="info"><h2 class="n">36. <a href="/los-angeles-ca/mip/the-coffee-table-eagle-rock-11287432?lid=11287432" data-analytics="{"target":"name","feature_click":""}" rel="" class="business-name" data-impressed="1"><span itemprop="name">The Coffee Table Eagle Rock</span></a></h2><div data-tripadvisor="{"rating":"4.0","count":"11"}" 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="{"click_id":22,"listing_features":"ratings"}" 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="{"click_id":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, </span><span itemprop="addressRegion">CA</span> <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="{"click_id":1171,"adclick":false,"listing_features":"category","events":""}" data-impressed="1">Coffee & Espresso Restaurants</a><a href="/los-angeles-ca/bars" data-analytics="{"click_id":1171,"adclick":false,"listing_features":"category","events":""}" data-impressed="1">Bars</a></div><div class="links"><a href="http://www.coffeetablelounge.com" rel="nofollow" target="_blank" data-analytics="{"click_id":6,"act":2,"dku":"http://www.coffeetablelounge.com","FL":"url","target":"website","LOC":"http://www.coffeetablelounge.com","adclick":true}" class="track-visit-website" data-impressed="1">Website</a><a href="/listings/11287432/menu?lid=11287432" data-analytics="{"click_id":1614,"target":"menus","listing_features":"menu-link"}" class="menu" data-impressed="1">Menu</a></div><a data-analytics="{"adclick":true,"events":"event7,event6","category":"8004238","impression_id":"fbd98612-6b8a-43c2-b31e-fd579de20126","listing_id":"11287432","item_id":-1,"listing_type":"free","ypid":"11287432","content_provider":"MDM","srid":"L-webyp-1c6db222-cc63-48d8-90d1-2d5dc8754cca-11287432","item_type":"PUP","lhc":"8004238","ldir":"LA","rate":3.5,"hasTripAdvisor":true,"mip_claimed_staus":"mip_unclaimed","mip_ypid":"11287432","click_id":523,"listing_features":"orderonline"}" href="https://yellowpages.pingup.com/Bkm3xG?ypid=11287432&uvid=t3pfPllxtLYkH2dlkSbiCC1marvZprsz1YhqhycO80NYrDv0OMX3uTJ3ryFG464RywmpWCrB&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 :(得分: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()')