Issue
With an assitance I have written a scipt in scrapy, which should previously read excel file -> take plate number then put it plate_num_xlsx
variable. Although for not being an empty I have assigned a value to it. But the logic is to take one by one the values from excel file column A with pandas then invoke parse() function and inside parse function `
plate = row.css('a::text').get()
price = row.css('p::text').get()
if plate_num_xlsx==plate.replace(" ","")
` if taken value is equal parsed value from website value it should print it.
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=¤tmatches=&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()
The problem with scrapy is it does not give you traceback error. In Selenium or other scripts if debug screen was in red, I could easily understand what is an issue. Here it only writes the output all in red color. Cannot understand on which line there is a problem
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=¤tmatches=&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
Solution
Well, there is no error. The reason it's not yielding items is that plate = row.css('a::text').get()
has some extra tabs and maybe a new line at the end. you just need to add strip
in the condition.
...
if plate_num_xlsx==plate.replace(" ","").strip():
...
Answered By - zaki98
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.