Reputation: 154
I am trying to scrape some data off a company register, So far it works to scrape each search result, however when i try exporting it. it show a null objects after each search result, as though it scrapes the same page twice?
THis is a snippet of the log.
2019-05-14 08:19:21 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.companiesintheuk.co.uk/ltd/a-c-1> (referer: https://www.companiesintheuk.co.uk/Company/Find?q=a)
2019-05-14 08:19:21 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.companiesintheuk.co.uk/ltd/a-c-1>
{'location': u'BEANCROFT ROAD', 'postal_code': None, 'company_name': u'A C PLC', 'address': u'BEANCROFT FARM'}
2019-05-14 08:19:21 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.companiesintheuk.co.uk/ltd/a-c-1>
{'location': None, 'postal_code': None, 'company_name': None, 'address': None}
And finally my code
import scrapy
import re
from scrapy.linkextractors import LinkExtractor
class QuotesSpider(scrapy.Spider):
name = 'CYRecursive'
start_urls = [
'https://www.companiesintheuk.co.uk/Company/Find?q=a']
def parse(self, response):
for company_url in response.xpath('//div[@class="search_result_title"]/a/@href').extract():
yield scrapy.Request(
url=response.urljoin(company_url),
callback=self.parse_details,
)
def parse_details(self, response):
# Looping throught the searchResult block and yielding it
for i in response.css('div.col-md-6'):
yield {
'company_name': i.css('#content2 > strong:nth-child(2) > strong:nth-child(1) > div:nth-child(1)::text').get(),
'address': i.css("#content2 > strong:nth-child(2) > address:nth-child(2) > div:nth-child(1) > span:nth-child(1)::text").extract_first(),
'location': i.css("#content2 > strong:nth-child(2) > address:nth-child(2) > div:nth-child(1) > span:nth-child(3)::text").extract_first(),
'postal_code': i.css("#content2 > strong:nth-child(2) > address:nth-child(2) > div:nth-child(1) > a:nth-child(5) > span:nth-child(1)::text").extract_first(),
}
Thank you in advance!
Upvotes: 0
Views: 54
Reputation: 3717
You have two elements div.col-md-6
one each company page (example: https://www.companiesintheuk.co.uk/ltd/a-c-1). So, first has company details and second one contains map and no company data.
So, you can modify you code with:
def parse_details(self, response):
for i in response.css('div.col-md-6'):
if not i.css('#content2 > strong:nth-child(2) > strong:nth-child(1)'):
continue
yield {
'company_name': i.css('#content2 > strong:nth-child(2) > strong:nth-child(1) > div:nth-child(1)::text').get(),
'address': i.css("#content2 > strong:nth-child(2) > address:nth-child(2) > div:nth-child(1) > span:nth-child(1)::text").extract_first(),
'location': i.css("#content2 > strong:nth-child(2) > address:nth-child(2) > div:nth-child(1) > span:nth-child(3)::text").extract_first(),
'postal_code': i.css("#content2 > strong:nth-child(2) > address:nth-child(2) > div:nth-child(1) > a:nth-child(5) > span:nth-child(1)::text").extract_first(),
}
So, just skip items that initially do not have needed block.
Upvotes: 2