My items.py file goes like this:
from scrapy.item import Item, Field
class SpiItem(Item):
title = Field()
lat = Field()
lng = Field()
add = Field()
and the spider is:
import scrapy
import re
from spi.items import SpiItem
class HdfcSpider(scrapy.Spider):
name = "hdfc"
allowed_domains = ["hdfc.com"]
start_urls = ["http://hdfc.com/branch-locator"]
def parse(self,response):
addresses = response.xpath('//script')
for sel in addresses:
item = SpiItem()
item['title'] = sel.xpath('//script[@type="text/javascript"][1]').re('(?<="title":).+(?=")')
item['lat'] = sel.xpath('//script[@type="text/javascript"][1]').re('(?<="latitude":).+(?=")')
item['lng'] = sel.xpath('//script[@type="text/javascript"][1]').re('(?<="longitude":).+(?=")')
item['add'] = sel.xpath('//script[@type="text/javascript"][1]').re('(?<="html":).+(?=")')
yield item
The whole javascript code, on viewing page source, is written inside: //html/body/table/tbody/tr[348]/td[2].
Why is my code not working? I want to extract just the four fields mentioned in the items file.