【问题标题】:Python Scrapy: Yahoo Fantasy spider returning nothing, no errorsPython Scrapy:Yahoo Fantasy 蜘蛛什么都不返回,没有错误
【发布时间】:2012-09-17 16:07:25
【问题描述】:

我正在开展一个项目,从各种服务的 Fantasy Football 联赛中收集统计数据,而雅虎是我目前所困的那个。我希望我的蜘蛛抓取公共雅虎联盟的草稿结果页面。当我运行蜘蛛时,它没有给我任何结果,也没有错误消息。它只是说:

2012-09-14 17:29:08-0700 [draft] DEBUG: Crawled (200) <GET http://football.fantasysports.yahoo.com/f1/753697/draftresults?drafttab=round> (referer: None)
2012-09-14 17:29:08-0700 [draft] INFO: Closing spider (finished)
2012-09-14 17:29:08-0700 [draft] INFO: Dumping spider stats:
    {'downloader/request_bytes': 250,
     'downloader/request_count': 1,
     'downloader/request_method_count/GET': 1,
     'downloader/response_bytes': 48785,
     'downloader/response_count': 1,
     'downloader/response_status_count/200': 1,
     'finish_reason': 'finished',
     'finish_time': datetime.datetime(2012, 9, 15, 0, 29, 8, 734000),
     'scheduler/memory_enqueued': 1,
     'start_time': datetime.datetime(2012, 9, 15, 0, 29, 7, 718000)}
2012-09-14 17:29:08-0700 [draft] INFO: Spider closed (finished)
2012-09-14 17:29:08-0700 [scrapy] INFO: Dumping global stats:
    {}

这不是登录问题,因为有问题的页面无需登录即可访问。我从此处发布的其他问题中看到,人们已经在雅虎的其他部门工作。雅虎幻想是否有可能阻止蜘蛛?我已经为 ESPN 成功编写了一个,所以我认为问题不在于我的代码。反正就是这样:

class DraftSpider(CrawlSpider):
name = "draft"
#psycopg stuff here

rows = ["753697"]

allowed_domains = ["football.fantasysports.yahoo.com"]

start_urls = []

for row in rows:

    start_urls.append("http://football.fantasysports.yahoo.com/f1/" + "%s" % (row) + "/draftresults?drafttab=round")

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        sites = hxs.select("/html/body/div/div/div/div/div/div/div/table/tr")
        items = []
        for site in sites:
            item = DraftItem()
            item['pick_number'] = site.select("td[@class='first']/text()").extract()
            item['pick_player'] = site.select("td[@class='player']/a/text()").extract()
            item['pick_nflteam'] = site.select("td[@class='player']/span/text()").extract()
            item['pick_ffteam'] = site.select("td[@class='last']/@title").extract()
            items.append(item)
        return items

非常感谢您对此的任何见解。

【问题讨论】:

  • 1.覆盖start_requests 而不是填充start_urls; 2.调试你的代码。按照逻辑打印一些内容:它是否到达parse 方法? xpath 查询是否有效?
  • 1.尝试scrapy shell &lt;url&gt; 检查 XPath 选择器是否工作。 2. 将CrawlSpider 与自定义parse 方法一起使用没有意义,因为CrawlSpider 有自己的parse 定义。 BaseSpider 会更合适。 3. 此外,它可能只是缩进,但在上面似乎您在for 循环内定义parse 方法,并在每次迭代时覆盖它。

标签: python yahoo scrapy


【解决方案1】:
C:\Users\Akhter Wahab>scrapy shell http://football.fantasysports.yahoo.com/f1/75
In [1]: hxs.select("/html/body/div/div/div/div/div/div/div/table/tr")
Out[1]: []

你的绝对 Xpath 不正确 "/html/body/div/div/div/div/div/div/div/table/tr"

我永远不会推荐你使用绝对Xpath,但你应该使用一些相对xpath,就像所有结果都在

//div[@id='drafttables']

这个 div。这样您就可以开始获得结果了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多