Scrapy在shell中运行正常但不输出结果。

huangapple go评论78阅读模式
英文:

Scrapy not giving output but works in shell

问题

这是我的 Scrapy 代码块:

import scrapy

class MedspiderSpider(scrapy.Spider):
    name = "medspider"
    allowed_domains = ["www.1mg.com"]
    start_urls = ["https://www.1mg.com/drugs-all-medicines"]

    def parse(self, response):
        meds = response.css('div.style__flex-1___A_qoj')

        for med in meds:
            yield{
                'name' : med.css('div div::text').get(),
                'price' : med.css('div:has(> span)::text').getall()[-1],
                'strip content' :  med.css('div::text').getall()[-4],
                'manufacturer' :  med.css('div::text').getall()[-3]
            }

在运行时使用 scrapy crawl medspider,它会给出以下输出:

2023-05-28 11:44:22 [scrapy.utils.log] INFO: Scrapy 2.9.0 started (bot: tata1mg)
...(中间省略)
2023-05-28 11:44:23 [scrapy.extensions.telnet] INFO: Telnet Password: 74ffa37a1d23bc25
...(中间省略)
2023-05-28 11:44:23 [scrapy.core.engine] INFO: Spider opened
2023-05-28 11:44:23 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2023-05-28 11:44:23 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2023-05-28 11:44:23 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.1mg.com/robots.txt> (referer: None)
2023-05-28 11:44:23 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.1mg.com/drugs-all-medicines> (referer: None)
2023-05-28 11:44:23 [scrapy.core.engine] INFO: Closing spider (finished)
...(中间省略)
2023-05-28 11:44:23 [scrapy.core.engine] INFO: Spider closed (finished)

但在 shell 中运行代码时,它会给出以下输出:

In [21]: med.css('div div div ::text').get()
Out[21]: 'Augmentin 625 Duo Tablet'

In [25]: med.css('div::text').getall()[-4]
Out[25]: 'strip of 10 tablets'

In [26]: med.css('div::text').getall()[-3]
Out[26]: 'Glaxo SmithKline Pharmaceuticals Ltd'
英文:

So i'm new to web scraping and i've been trying to scrape this website but the problem i'm encountering is that when i run my code in shell it runs but it doesn't when i run it as a whole scrapy code.

Here is my Scrapy code block:

import scrapy


class MedspiderSpider(scrapy.Spider):
    name = &quot;medspider&quot;
    allowed_domains = [&quot;www.1mg.com&quot;]
    start_urls = [&quot;https://www.1mg.com/drugs-all-medicines&quot;]

    def parse(self, response):
        meds = response.css(&#39;div.style__flex-1___A_qoj&#39;)
        
        for med in meds:
            yield{
                &#39;name&#39; : med.css(&#39;div div::text&#39;).get(),
                &#39;price&#39; : med.css(&#39;div:has(&gt; span)::text&#39;).getall()[-1],
                &#39;strip content&#39; :  med.css(&#39;div::text&#39;).getall()[-4],
                &#39;manufacturer&#39; :  med.css(&#39;div::text&#39;).getall()[-3]
            }

Running this code using scrapy crawl medspider it gives the following output:-

2023-05-28 11:44:22 [scrapy.utils.log] INFO: Scrapy 2.9.0 started (bot: tata1mg)
2023-05-28 11:44:22 [scrapy.utils.log] INFO: Versions: lxml 4.9.2.0, libxml2 2.9.12, cssselect 1.2.0, parsel 1.8.1, w3lib 2.1.1, Twisted 22.10.0, Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)], pyOpenSSL 23.1.1 (OpenSSL 3.1.0 14 Mar 2023), cryptography 40.0.2, Platform Windows-10-10.0.22624-SP0
2023-05-28 11:44:22 [scrapy.crawler] INFO: Overridden settings:
{&#39;BOT_NAME&#39;: &#39;tata1mg&#39;,
 &#39;FEED_EXPORT_ENCODING&#39;: &#39;utf-8&#39;,
 &#39;NEWSPIDER_MODULE&#39;: &#39;tata1mg.spiders&#39;,
 &#39;REQUEST_FINGERPRINTER_IMPLEMENTATION&#39;: &#39;2.7&#39;,
 &#39;ROBOTSTXT_OBEY&#39;: True,
 &#39;SPIDER_MODULES&#39;: [&#39;tata1mg.spiders&#39;],
 &#39;TWISTED_REACTOR&#39;: &#39;twisted.internet.asyncioreactor.AsyncioSelectorReactor&#39;}
2023-05-28 11:44:22 [asyncio] DEBUG: Using selector: SelectSelector
2023-05-28 11:44:22 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.asyncioreactor.AsyncioSelectorReactor
2023-05-28 11:44:22 [scrapy.utils.log] DEBUG: Using asyncio event loop: asyncio.windows_events._WindowsSelectorEventLoop
2023-05-28 11:44:22 [scrapy.extensions.telnet] INFO: Telnet Password: 74ffa37a1d23bc25
2023-05-28 11:44:22 [scrapy.middleware] INFO: Enabled extensions:
[&#39;scrapy.extensions.corestats.CoreStats&#39;,
 &#39;scrapy.extensions.telnet.TelnetConsole&#39;,
 &#39;scrapy.extensions.logstats.LogStats&#39;]
2023-05-28 11:44:22 [scrapy.middleware] INFO: Enabled downloader middlewares:
[&#39;scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.useragent.UserAgentMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.retry.RetryMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.redirect.RedirectMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.cookies.CookiesMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware&#39;,
 &#39;scrapy.downloadermiddlewares.stats.DownloaderStats&#39;]
2023-05-28 11:44:22 [scrapy.middleware] INFO: Enabled spider middlewares:
[&#39;scrapy.spidermiddlewares.httperror.HttpErrorMiddleware&#39;,
 &#39;scrapy.spidermiddlewares.offsite.OffsiteMiddleware&#39;,
 &#39;scrapy.spidermiddlewares.referer.RefererMiddleware&#39;,
 &#39;scrapy.spidermiddlewares.urllength.UrlLengthMiddleware&#39;,
 &#39;scrapy.spidermiddlewares.depth.DepthMiddleware&#39;]
2023-05-28 11:44:22 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2023-05-28 11:44:22 [scrapy.core.engine] INFO: Spider opened
2023-05-28 11:44:23 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2023-05-28 11:44:23 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2023-05-28 11:44:23 [scrapy.core.engine] DEBUG: Crawled (200) &lt;GET https://www.1mg.com/robots.txt&gt; (referer: None)
2023-05-28 11:44:23 [scrapy.core.engine] DEBUG: Crawled (200) &lt;GET https://www.1mg.com/drugs-all-medicines&gt; (referer: None)
2023-05-28 11:44:23 [scrapy.core.engine] INFO: Closing spider (finished)
2023-05-28 11:44:23 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{&#39;downloader/request_bytes&#39;: 896,
 &#39;downloader/request_count&#39;: 2,
 &#39;downloader/request_method_count/GET&#39;: 2,
 &#39;downloader/response_bytes&#39;: 48250,
 &#39;downloader/response_count&#39;: 2,
 &#39;downloader/response_status_count/200&#39;: 2,
 &#39;elapsed_time_seconds&#39;: 0.670526,
 &#39;finish_reason&#39;: &#39;finished&#39;,
 &#39;finish_time&#39;: datetime.datetime(2023, 5, 28, 6, 14, 23, 763883),
 &#39;httpcompression/response_bytes&#39;: 264903,
 &#39;httpcompression/response_count&#39;: 2,
 &#39;log_count/DEBUG&#39;: 5,
 &#39;log_count/INFO&#39;: 10,
 &#39;response_received_count&#39;: 2,
 &#39;robotstxt/request_count&#39;: 1,
 &#39;robotstxt/response_count&#39;: 1,
 &#39;robotstxt/response_status_count/200&#39;: 1,
 &#39;scheduler/dequeued&#39;: 1,
 &#39;scheduler/dequeued/memory&#39;: 1,
 &#39;scheduler/enqueued&#39;: 1,
 &#39;scheduler/enqueued/memory&#39;: 1,
 &#39;start_time&#39;: datetime.datetime(2023, 5, 28, 6, 14, 23, 93357)}
2023-05-28 11:44:23 [scrapy.core.engine] INFO: Spider closed (finished)

But when i run the code in shell it gives the output like:-

In [21]: med.css(&#39;div div div ::text&#39;).get()
Out[21]: &#39;Augmentin 625 Duo Tablet&#39;
In [25]: med.css(&#39;div::text&#39;).getall()[-4]
Out[25]: &#39;strip of 10 tablets&#39;
In [26]: med.css(&#39;div::text&#39;).getall()[-3]
Out[26]: &#39;Glaxo SmithKline Pharmaceuticals Ltd&#39;

答案1

得分: 1

在你的settings.py文件中,或者在你的爬虫模块中使用custom_settings属性,分配一个自定义的USER_AGENT值。

例如:

settings.py

USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"

或者在你的爬虫模块中:

...

class MedspiderSpider(scrapy.Spider):
    name = "medspider"
    allowed_domains = ["www.1mg.com"]
    start_urls = ["https://www.1mg.com/drugs-all-medicines"]
    custom_settings = {
        "USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
    }

...

然后尝试再次运行 scrapy crawl ...

英文:

Either in your settings.py file, or using your spiders custom_settings attribute, assign a custom USER_AGENT value.

For example:

settings.py

USER_AGENT = &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36&quot;

or in your spider module:

...

class MedspiderSpider(scrapy.Spider):
    name = &quot;medspider&quot;
    allowed_domains = [&quot;www.1mg.com&quot;]
    start_urls = [&quot;https://www.1mg.com/drugs-all-medicines&quot;]
    custom_settings = {
        &quot;USER_AGENT&quot;: &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36&quot;
    }

...

And then try running scrapy crawl ... again

huangapple
  • 本文由 发表于 2023年5月28日 14:27:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定