I don't know what I am doing wrong. I am trying to extract text and store it in a list. In firebug and firepath when I enter the path it shows exact correct text. But when I apply it returns empty list. I am Trying to scrape www.insider.in/mumbai. it goes to all the links and scrape the event title,address and other information. Here is my new edited code:
from scrapy.spider import BaseSpider
from scrapy.selector import Selector
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from scrapy.selector import HtmlXPathSelector
import time
import requests
import csv
class insiderSpider(BaseSpider):
name = 'insider'
allowed_domains = ["insider.in"]
start_urls = ["http://www.insider.in/mumbai/"]
def parse(self,response):
driver = webdriver.Firefox()
print response.url
driver.get(response.url)
s = Selector(response)
#hxs = HtmlXPathSelector(response)
source_link = []
temp = []
title =""
Price = ""
Venue_name = ""
Venue_address = ""
description = ""
event_details = []
alllinks = s.xpath('//div[@class="bottom-details-right"]//a/@href').extract()
print alllinks
length_of_alllinks = len(alllinks)
for single_event in range(1,length_of_alllinks):
if "https://insider.in/event" in alllinks[single_event]:
source_link.append(alllinks[single_event])
driver.get(alllinks[single_event])
s = Selector(response)
#hxs = HtmlXPathSelector(response)
time.sleep(3)
title = s.xpath('//div[@class = "cell-title in-headerTitle"]/h1//text()').extract()
print title
temp = s.xpath('//div[@class = "cell-caption centered in-header"]//h3//text()').extract()
print temp
time.sleep(2)
a = len(s.xpath('//div[@class = "bold-caption price"]//text()').extract())
if a > 0:
Price = s.xpath('//div[@class = "bold-caption price"]//text()').extract()
time.sleep(2)
else:
Price = "RSVP"
time.sleep(2)
print Price
Venue_name = s.xpath('//div[@class = "address"]//div[@class = "section-title"]//text()').extract()
print Venue_name
Venue_address = s.xpath('//div[@class ="address"]//div//text()[preceding-sibling::br]').extract()
print Venue_address
description = s.xpath('//div[@class="cell-caption accordion-padding"]//text()').extract()
print description
time.sleep(5)
event_details.append([title,temp,Price,Venue_name,Venue_address,description])
else:
print "Other part"
Edited Output:
[u'https://insider.in/weekender-music-festival-2015', u'https://insider.in/event/east-india-comedy-presents-back-benchers#', u'https://insider.in/event/art-of-story-telling', u'https://insider.in/feelings-in-india-with-kanan-gill', u'https://insider.in/event/the-tall-tales-workshop-capture-your-story', u'https://insider.in/halloween-by-the-pier-2015', u'https://insider.in/event/whats-your-story', u'https://insider.in/event/beyond-contemporary-art']
2015-08-03 12:53:29 [selenium.webdriver.remote.remote_connection] DEBUG: POST http://127.0.0.1:60924/hub/session/f675b909-5515-41d4-a89e-d197c296023d/url {"url": "https://insider.in/event/east-india-comedy-presents-back-benchers#", "sessionId": "f675b909-5515-41d4-a89e-d197c296023d"}
2015-08-03 12:53:29 [selenium.webdriver.remote.remote_connection] DEBUG: Finished Request
[]
[]
RSVP
[]
[]
[]
[[[], [], 'RSVP', [], [], []]]
Even the if condition fails and prints RSVP. I don't seem to understand what I am doing wrong. I am stuck in this part since 3 days. Please help.