简体   繁体   English

Scrapy XPath在Python中返回空列表

[英]Scrapy xpath returning empty list in python

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. 在firebug和firepath中,当我输入路径时,它会显示完全正确的文本。 But when I apply it returns empty list. 但是,当我应用它返回空列表。 I am Trying to scrape www.insider.in/mumbai. 我正在尝试抓取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. 即使if条件失败并打印RSVP。 I don't seem to understand what I am doing wrong. 我似乎不明白我在做什么错。 I am stuck in this part since 3 days. 自3天以来,我一直停留在这一部分。 Please help. 请帮忙。

I removed things like webdriver and got a basic code that works 我删除了诸如webdriver之类的东西,并获得了可以正常工作的基本代码

import scrapy
import logging
from scrapy.http import Request
from scrapy.selector import Selector

class insiderSpider(scrapy.Spider):
    name = 'insider'
    allowed_domains = ["insider.in"]
    start_urls = ["http://www.insider.in/mumbai/"]
    event_details = list() # Changed. Now event_detail is a menber data of class

    def parse(self, response):
        source_link = []
        temp = []
        title =""
        Price = ""
        Venue_name = ""
        Venue_address = ""
        description = ""
        alllinks = response.xpath('//div[@class="bottom-details-right"]//a/@href').extract()
        print alllinks
        for single_event in alllinks:
            if "https://insider.in/event" in single_event:
                yield Request(url = single_event, callback = self.parse_event)
            else:
                print 'Other part'

    def parse_event(self, response):
        title = response.xpath('//div[@class = "cell-title in-headerTitle"]/h1//text()').extract()
        print title
        temp = response.xpath('//div[@class = "cell-caption centered in-header"]//h3//text()').extract()
        print temp
        a = len(response.xpath('//div[@class = "bold-caption price"]//text()').extract())
        if a > 0:
            Price = response.xpath('//div[@class = "bold-caption price"]//text()').extract()
        else:
            Price = "RSVP"
        print Price
        Venue_name = response.xpath('normalize-space(//div[@class = "address"]//div[@class = "section-title"]//text())').extract()

        print Venue_name
        Venue_address = response.xpath('normalize-space(//div[@class ="address"]//div//text()[preceding-sibling::br])').extract()

        print Venue_address
        description = response.xpath('normalize-space(//div[@class="cell-caption accordion-padding"]//text())').extract()

        print description
        self.event_details.append([title,temp,Price,Venue_name,Venue_address,description]) # Notice that event_details is used as self.event_details ie, using member data
        print self.event_details # Here also self.event_details

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM