Scrapy:搜寻器无法爬行

时间:2019-01-31 03:17:47

标签: python web-scraping scrapy web-crawler

我显然是python,scrapy和一般编程的新手。我正在尝试抓取此网站,但我的代码似乎无法正常工作。我发现的所有示例和教程都涉及简单的普通网站。也许我只是无法解决这个问题。我需要抓取数百个结果,但我真的不想手动执行。

因此在这种情况下,我只是试图仅从div对象获取href来检查其是否有效。没有。

import scrapy
import requests


class QuotesSpider(scrapy.Spider):
    name = "items"

    def start_requests(self):
        urls = [
            'https://www.bosch-professional.com/ar/es/dl/localizador-de-distribuidores/dealerslist/',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        list_doc = open('list_doc.txt', 'w')
        for item in response.css('div.row.m-dealer_list__row'):
            yield {
                'text': item.css('a::attr(href)').extract(),

            }

在终端上运行(忽略机器人)时,它返回:

2019-01-30 23:57:13 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.memusage.MemoryUsage',
 'scrapy.extensions.logstats.LogStats']
2019-01-30 23:57:13 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2019-01-30 23:57:13 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2019-01-30 23:57:13 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2019-01-30 23:57:13 [scrapy.core.engine] INFO: Spider opened
2019-01-30 23:57:13 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2019-01-30 23:57:13 [scrapy.extensions.telnet] DEBUG: Telnet console listening on #NUMBER
2019-01-30 23:57:16 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.bosch-professional.com/ar/es/dl/localizador-de-distribuidores/dealerslist/> (referer: None)
2019-01-30 23:57:16 [scrapy.core.engine] INFO: Closing spider (finished)
2019-01-30 23:57:16 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 276,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 70592,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2019, 1, 31, 2, 57, 16, 541215),
 'log_count/DEBUG': 2,
 'log_count/INFO': 7,
 'memusage/max': 57974784,
 'memusage/startup': 57974784,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2019, 1, 31, 2, 57, 13, 861593)}
2019-01-30 23:57:16 [scrapy.core.engine] INFO: Spider closed (finished)

感谢您提供的任何帮助。

3 个答案:

答案 0 :(得分:1)

据我所见,页面上确实没有这样的元素:

In [2]: fetch("https://www.bosch-professional.com/ar/es/dl/localizador-de-distribuidores/dealerslist/")
2019-01-31 09:31:47 [scrapy.core.engine] INFO: Spider opened
2019-01-31 09:31:48 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.bosch-professional.com/ar/es/dl/localizador-de-distribuidores/dealerslist/> (referer: None, latency: 0.87 s)

In [3]: response.css('div.row.m-dealer_list__row')
Out[3]: []

但是,如果您尝试:

In [4]: response.css('div.m-dealer_citylist__card a::text').extract()
Out[4]: 
[u'25 DE MAYO - BS AS',
 u'25 DE MAYO - LA PAMP',
 u'25 DE MAYO',
 u'9 DE ABRIL',
...
 u'ZENON PEREYRA',
 u'Z\xc1RATE']

答案 1 :(得分:0)

我曾参观过您要报废的网站,但你的CSS似乎并不被匹配HTML的任何属性。

没有任何具有类名m-dealer_list__row的标签

我看到的是m-dealer_citylist

如果我描述您的CSS:

您的css描述您正在提取具有2个类的div元素,一个是row,第二个是m-dealer_list__row

如果您想使用div类的row,那么在此div中使用m-dealer_list__row的任何标签都可以尝试:

"div.row .m-dealer_list__row"

答案 2 :(得分:0)

这是给你的)。自己了解

import scrapy
import requests


class QuotesSpider(scrapy.Spider):
    name = "items"

    def start_requests(self):
        urls = [
            'https://www.bosch-professional.com/ar/es/dl/localizador-de-distribuidores/dealerslist/',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        list_doc = open('list_doc.txt', 'w')
        for item in response.css('.trackingTeaser'):
            href = item.css('a::attr(href)').extract_first()
            href = response.urljoin(href)
            list_doc.write(''.join(item.css('a::text').re(r'([^ \n\t]+)')) + ': ' + href + '\n')
        list_doc.close()

输出:

enter image description here