2014-05-16 105 views
1

我需要从给出锚定标记的特定文本的href属性中提取url。使用Xpath提取锚定标记的href给定文本

from scrapy.spider import Spider 
from scrapy.selector import Selector 
from nba.items import NBAItem 

class ESPNSpider(Spider): 
    name = "ESPN" 
    allowed_domains = ["espn.com"] 
    start_urls = ["http://espn.go.com/nba/teams"] 

def parse(self, response): 
    sel = Selector(response) 
    sites = sel.xpath('//*[@id="content"]/div[3]/div[1]') 
    items = [] 
    for site in sites: 
     item = NBAItem() 
     item['team_name'] = site.xpath('//a[@class="bi"]/text()').extract() 
     item['team_link'] = site.xpath('//a[@class="bi"]/@href').extract() 
     item['team_stats_link'] = site.xpath('//a[text()='Stats']/@href').extract() 
     items.append(item) 
    return items 

这是我遇到的麻烦线路:

item['team_stats_link'] = site.xpath('//a[text()='Stats']/@href').extract() 

我也尝试:

item['team_stats_link'] = site.xpath('//a[contains(text(), 'Stats')]/@href).extract() 

相关网站:http://espn.go.com/nba/teams

+0

'site.xpath('// a [text()='Stats']/@ href')'是一个Python语法错误。看看单引号。 – Tomalak

+0

啊!一直以来。感谢您的帮助。 – user1636797

回答

1

你里面的XPath循环应该以.//开头,换句话说,您需要使其相关给site

我还经历li标签ulmedium-logos类中,而不是与content ID的div内第三div里面搜索第一个div

class ESPNSpider(Spider): 
    name = "ESPN" 
    allowed_domains = ["espn.com"] 
    start_urls = ["http://espn.go.com/nba/teams"] 

    def parse(self, response): 
     sel = Selector(response) 
     sites = sel.xpath('//ul[@class="medium-logos"]//li') 
     for site in sites: 
      item = NBAItem() 
      item['team_name'] = site.xpath('.//a[@class="bi"]/text()').extract()[0] 
      item['team_link'] = site.xpath('.//a[@class="bi"]/@href').extract()[0] 
      item['team_stats_link'] = site.xpath(".//a[text()='Stats']/@href").extract()[0] 
      yield item 

它产生:

{'team_link': u'http://espn.go.com/nba/team/_/name/bos/boston-celtics', 'team_name': u'Boston Celtics', 'team_stats_link': u'/nba/teams/stats?team=bos'} 
{'team_link': u'http://espn.go.com/nba/team/_/name/bkn/brooklyn-nets', 'team_name': u'Brooklyn Nets', 'team_stats_link': u'/nba/teams/stats?team=bkn'} 
... 
+0

你介意解释为什么你会选择相对路径?另外,为什么要通过'中等标识'类而不是'bi'类? – user1636797

+0

@ user1636797我仍然使用'bi'来查找团队的名称和链接。你需要把它做成相对的,这样你才能在团队内搜索而不是整个页面。希望有所帮助。 – alecxe

+0

我明白你的意思了。谢谢! – user1636797

相关问题