scrapy python无法使用条件进行分析

mrphzbgm  于 2023-01-17  发布在  Python
关注(0)|答案(1)|浏览(139)

在一个助手的帮助下,我已经写了一个scipt在scraby,它应该事先读取excel文件-〉采取板号,然后把它放在plate_num_xlsx变量。虽然不是一个空的,我已经分配了一个值给它。但逻辑是采取一个接一个的值从excel文件列A与Pandas,然后调用parse()函数和内部parse函数

plate = row.css('a::text').get()
    price = row.css('p::text').get()
    if plate_num_xlsx==plate.replace(" ","")

'如果所取值等于从网站值解析的值,则应打印该值。

import scrapy
from scrapy.crawler import CrawlerProcess
import pandas as pd

plate_num_xlsx = 'LA55ERR'

class plateScraper(scrapy.Spider):
    name = 'scrapePlate'
    allowed_domains = ['dvlaregistrations.direct.gov.uk']
    start_urls = [f"https://dvlaregistrations.dvla.gov.uk/search/results.html?search={plate_num_xlsx}&action=index&pricefrom=0&priceto=&prefixmatches=&currentmatches=&limitprefix=&limitcurrent=&limitauction=&searched=true&openoption=&language=en&prefix2=Search&super=&super_pricefrom=&super_priceto="]

    def read_xlsx(self):
        df=pd.read_excel('data.xlsx')
        columnA_values=df['PLATES']
        for row in columnA_values:
            plate_num_xlsx=row
            yield scrapy.Request(start_urls.format(row))

    def parse(self, response):
        for row in response.css('div.resultsstrip'):
            plate = row.css('a::text').get()
            price = row.css('p::text').get()
            if plate_num_xlsx==plate.replace(" ",""):
                print(plate.replace(" ", ""))
                yield {"plate": plate.strip(), "price": price.strip()}

process = CrawlerProcess()
process.crawl(plateScraper)
process.start()

scrappy的问题是它不会给予你回溯错误。在Selenium或其他脚本中,如果调试屏幕是红色的,我可以很容易地理解什么是一个问题。这里它只写输出都是红色的。不能理解哪一行有问题

C:\Users\Admin\AppData\Local\Programs\Python\Python310\python.exe C:/pythonPro/w_crawl/SimonDarak/scrpy_00.py 
2023-01-15 18:07:45 [scrapy.utils.log] INFO: Scrapy 2.7.1 started (bot: scrapybot)
2023-01-15 18:07:45 [scrapy.utils.log] INFO: Versions: lxml 4.9.1.0, libxml2 2.9.12, cssselect 1.2.0, parsel 1.7.0, w3lib 2.1.1, Twisted 22.10.0, Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)], pyOpenSSL 23.0.0 (OpenSSL 3.0.7 1 Nov 2022), cryptography 39.0.0, Platform Windows-10-10.0.19044-SP0
2023-01-15 18:07:45 [scrapy.crawler] INFO: Overridden settings:
{}
2023-01-15 18:07:45 [py.warnings] WARNING: C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\scrapy\utils\request.py:231: ScrapyDeprecationWarning: '2.6' is a deprecated value for the 'REQUEST_FINGERPRINTER_IMPLEMENTATION' setting.

It is also the default value. In other words, it is normal to get this warning if you have not defined a value for the 'REQUEST_FINGERPRINTER_IMPLEMENTATION' setting. This is so for backward compatibility reasons, but it will change in a future version of Scrapy.

See the documentation of the 'REQUEST_FINGERPRINTER_IMPLEMENTATION' setting for information on how to handle this deprecation.
  return cls(crawler)

2023-01-15 18:07:45 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
2023-01-15 18:07:45 [scrapy.extensions.telnet] INFO: Telnet Password: adce4fc71429f9ef
2023-01-15 18:07:45 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.logstats.LogStats']
2023-01-15 18:07:46 [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']
2023-01-15 18:07:46 [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']
2023-01-15 18:07:46 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2023-01-15 18:07:46 [scrapy.core.engine] INFO: Spider opened
2023-01-15 18:07:46 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2023-01-15 18:07:46 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2023-01-15 18:07:47 [filelock] DEBUG: Attempting to acquire lock 2802896714000 on C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\tldextract\.suffix_cache/publicsuffix.org-tlds\de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2023-01-15 18:07:47 [filelock] DEBUG: Lock 2802896714000 acquired on C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\tldextract\.suffix_cache/publicsuffix.org-tlds\de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2023-01-15 18:07:47 [filelock] DEBUG: Attempting to release lock 2802896714000 on C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\tldextract\.suffix_cache/publicsuffix.org-tlds\de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2023-01-15 18:07:47 [filelock] DEBUG: Lock 2802896714000 released on C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\tldextract\.suffix_cache/publicsuffix.org-tlds\de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2023-01-15 18:07:47 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://dvlaregistrations.dvla.gov.uk/search/results.html?search=LA55ERR&action=index&pricefrom=0&priceto=&prefixmatches=&currentmatches=&limitprefix=&limitcurrent=&limitauction=&searched=true&openoption=&language=en&prefix2=Search&super=&super_pricefrom=&super_priceto=> (referer: None)
2023-01-15 18:07:47 [scrapy.core.engine] INFO: Closing spider (finished)
2023-01-15 18:07:47 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 461,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 11458,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'elapsed_time_seconds': 0.709073,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2023, 1, 15, 14, 7, 47, 140805),
 'httpcompression/response_bytes': 75657,
 'httpcompression/response_count': 1,
 'log_count/DEBUG': 6,
 'log_count/INFO': 10,
 '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(2023, 1, 15, 14, 7, 46, 431732)}
2023-01-15 18:07:47 [scrapy.core.engine] INFO: Spider closed (finished)

Process finished with exit code 0
xe55xuns

xe55xuns1#

好吧,没有错误。它没有生成项的原因是plate = row.css('a::text').get()有一些额外的制表符,并且可能在末尾有一个新行。您只需要在条件中添加strip

...
    if plate_num_xlsx==plate.replace(" ","").strip():
        ...

相关问题