【问题标题】:Scrapy not generating links properlyScrapy无法正确生成链接
【发布时间】:2014-03-01 07:48:00
【问题描述】:

我的爬行蜘蛛:

class FabulousFoxSpider(CrawlSpider):
    """docstring for EventsSpider"""
    name="fabulousfox"
    allowed_domains=["fabulousfox.com"]
    start_urls=["http://www.fabulousfox.com"]
    rules = (
        Rule(SgmlLinkExtractor(
            allow=(
                '/shows_page_(single|multi).aspx\?usID=(\d)*'
                ),
            unique=True),
            'parse_fabulousfox',
            ),
        )  

但是当我做scrapy crawl fabulousfox -o data.json -t json

我得到的输出为:

...................
......................
2014-03-01 13:11:56+0530 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2014-03-01 13:11:56+0530 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2014-03-01 13:11:57+0530 [fabulousfox] DEBUG: Crawled (200) <GET http://www.fabulousfox.com> (referer: None)
2014-03-01 13:11:57+0530 [fabulousfox] DEBUG: Crawled (403) <GET http://www.fabulousfox.com/../shows_page_multi.aspx?usID=365> (referer: http://www.fabulousfox.com)
2014-03-01 13:11:58+0530 [fabulousfox] DEBUG: Crawled (403) <GET http://www.fabulousfox.com/../shows_page_single.aspx?usID=389> (referer: http://www.fabulousfox.com)
2014-03-01 13:11:58+0530 [fabulousfox] DEBUG: Crawled (403) <GET http://www.fabulousfox.com/../shows_page_multi.aspx?usID=388> (referer: http://www.fabulousfox.com)
2014-03-01 13:11:58+0530 [fabulousfox] DEBUG: Crawled (403) <GET http://www.fabulousfox.com/../shows_page_single.aspx?usID=394> (referer: http://www.fabulousfox.com)
2014-03-01 13:11:58+0530 [fabulousfox] DEBUG: Crawled (403) <GET http://www.fabulousfox.com/../shows_page_multi.aspx?usID=358> (referer: http://www.fabulousfox.com)
2014-03-01 13:11:58+0530 [fabulousfox] INFO: Closing spider (finished)
2014-03-01 13:11:58+0530 [fabulousfox] INFO: Dumping Scrapy stats:
    {'downloader/request_bytes': 1660,
     'downloader/request_count': 6,
     'downloader/request_method_count/GET': 6,
     'downloader/response_bytes': 12840,
     'downloader/response_count': 6,
     'downloader/response_status_count/200': 1,
     'downloader/response_status_count/403': 5,
     'finish_reason': 'finished',
     'finish_time': datetime.datetime(2014, 3, 1, 7, 41, 58, 218296),
     'log_count/DEBUG': 8,
     'log_count/INFO': 7,
     'memdebug/gc_garbage_count': 0,
     'memdebug/live_refs/FabulousFoxSpider': 1,
     'memusage/max': 33275904,
     'memusage/startup': 33275904,
     'request_depth_max': 1,
     'response_received_count': 6,
     'scheduler/dequeued': 6,
     'scheduler/dequeued/memory': 6,
     'scheduler/enqueued': 6,
     'scheduler/enqueued/memory': 6,
     'start_time': datetime.datetime(2014, 3, 1, 7, 41, 56, 360266)}
2014-03-01 13:11:58+0530 [fabulousfox] INFO: Spider closed (finished)  

为什么生成的网址包含...
http://www.fabulousfox.com/../shows_page_multi.aspx?usID=365

而且它不会生成所有的 url。这里有什么问题?

【问题讨论】:

标签: python web-scraping web-crawler scrapy


【解决方案1】:

检查http://www.fabulousfox.com 的页面 HTML 源代码,您会注意到如下表格行:

<tr>
    <td width="7">
        <img src="images/home_shows_frame_left.jpg" width="7" height="128" />
    </td>
    <td width="155" height="128" align="center" valign="middle">
        <a id="Box4" href="../shows_page_single.aspx?usID=394"><img id="Image4" src="../images/ShowLogos/394.jpg" alt="Rickey Smiley's" style="border-width:0px;" /></a>
    </td>
    <td width="7" align="right">
        <img src="images/home_shows_frame_right.jpg" width="7" height="128" />
    </td>
</tr>

虽然浏览器会理解这些链接并将您引导至http://www.fabulousfox.com/shows_page_single.aspx?usID=394,但 Scrapy 的 SgmlLinkExtractor 将在内部使用 urlparse.urljoin()

>>> import urlparse
>>> urlparse.urljoin('http://www.fabulousfox.com/', '../shows_page_single.aspx?usID=394')
'http://www.fabulousfox.com/../shows_page_single.aspx?usID=394'
>>> 

您可以通过提供 process_value 可调用来帮助链接提取器,

SgmlLinkExtractor(process_value=lambda u: u.replace('../', '/'))

但它可能不会在所有情况下都满足您的需求

【讨论】:

  • 但前导点从何而来?他们为什么在那里?
  • 它们在网页的 HTML 中,来自网站。
  • 我弄错了。没有仔细看网址。无论如何,谢谢。
【解决方案2】:

您没有正确处理相关链接。

使用urlparse.urljoin 构建有效链接。

【讨论】:

  • 我不明白为什么我需要这样做?我写了一个爬虫,它在上面做同样的事情,但在旧版本的scrapy中。那里没有任何问题。
  • 我不想将链接从相对链接转换为绝对链接,反之亦然。就是这样,没有一个 URL 被抓取,甚至连起始的 URL 也没有。
  • 抓取了起始的(代码 200 = 成功),但所有其他的都不正确,因此会产生错误代码。当您访问不正确的 URL 时,通常会收到错误消息。
  • 是的,我明白了。但为什么 url 没有正确生成?我只写了几个这样的爬虫。他们在那里工作得很好。网址已正确创建。只是在这种情况下。为什么我需要像您在回答中所说的那样明确生成正确的网址?
  • 可能是因为这些网站没有在他们的网站中错误地使用 ../something 相对 URL。
猜你喜欢
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多