python - 没有页面在 scrapy 中被抓取

标签 python web-scraping scrapy

我在 Scrapy 中有以下蜘蛛,用于抓取和保存来自 eBay.com 的网页,用于雷朋太阳镜

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor

class EbaySpider(CrawlSpider):
    name = 'ebay'
    allowed_domains = ['ebay.com']
    start_urls = ['http://www.ebay.com',
        'http://www.ebay.com/sch/i.html?_from=R40&_trksid=p2050601.m570.l1312.R1.TR11.TRC2.A0.H0.Xray.TRS1&_nkw=ray+ban+sunglasses&_sacat=0'
    ]

    rules = [
        Rule(LinkExtractor(allow=[r'itm/.*ray.*ban.*sunglassses']), follow='true', callback='parse_item')
    ]    

    def parse_item(self, response):
        filename = response.url.split("/")[-2] + '.html'
        with open(filename, 'wb') as f:
            f.write(response.body)

这是我得到的输出:

INFO: Enabled item pipelines: 
2015-09-03 12:50:11 [scrapy] INFO: Spider opened
2015-09-03 12:50:11 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-09-03 12:50:11 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-09-03 12:50:12 [scrapy] DEBUG: Crawled (200) <GET 

http://www.ebay.com> (referer: None)
    2015-09-03 12:50:12 [scrapy] INFO: Closing spider (finished)
    2015-09-03 12:50:12 [scrapy] INFO: Dumping Scrapy stats:
    {'downloader/request_bytes': 210,
     'downloader/request_count': 1,
     'downloader/request_method_count/GET': 1,
     'downloader/response_bytes': 25707,
     'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2015, 9, 3, 19, 50, 12, 755020),
 'log_count/DEBUG': 2,
 'log_count/INFO': 7,
 'log_count/WARNING': 1,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2015, 9, 3, 19, 50, 11, 809426)}
2015-09-03 12:50:12 [scrapy] INFO: Spider closed (finished)

谁能指出哪里出了问题?我是 scrapy 的菜鸟。

最佳答案

页面上没有任何内容与字符串 r'itm/.*ray.*ban.*sunglassses' 中的正则表达式匹配。您可以通过启动一个指向您的 start_url 的 shell 并执行以下操作来对此进行测试:

>>> from scrapy.linkextractor import LinkExtractor
>>> LinkExtractor(allow=[r'itm/.*ray.*ban.*sunglassses']).extract_links(response)
[]

首先,要解决这个问题(这是罪魁祸首),是“太阳镜”中的错字(你有三个“s”)。

其次,正则表达式默认区分大小写,因此 r'ray.*ban' 不会匹配包含“Ray-ban”或“Ray Ban”的链接。要更正此问题,请使用 IGNORECASE 标志预编译您的正则表达式,并将其传递给您的链接提取器。

import re
...
rules = [
    Rule(LinkExtractor(allow=[re.compile(r'itm/.*ray.*ban.*sunglasses', re.IGNORECASE)]), 
         follow='true', callback='parse_item')
]

关于python - 没有页面在 scrapy 中被抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32384407/

相关文章:

python - 只选择 Postgresql 的两列并将其存储在 numpy 数组中

javascript - 使用 Node.js、request 和 Cheerio 从网站上抓取链接?

android - 在 Android 中抓取 HTML 网页的最快方法是什么?

java - Java中使用Selenium获取表内容

python - 在 Scrapy 中发送帖子请求

performance - 如何在 Scrapy 蜘蛛上运行 cProfiler

python - 如何在 contourf 的色标中设置最大值和最小值?

python - 在python中解析文件并修改字符串

python - 使用 Scipy 拟合 Weibull 分布

python - 来自同一页面中多个链接的同一项目中的scrapy数据?