Python 刮痧与硒

Python 刮痧与硒,python,selenium,scrapy,Python,Selenium,Scrapy,我有一个scrapy spider,它在一个网站上爬行,通过页面上的javascript重新加载内容。为了移动到下一个页面进行抓取,我一直在使用Selenium单击站点顶部的month链接 问题是,即使我的代码按预期在每个链接中移动,爬行器也只会根据月份数刮取第一个月(9月)的数据,并返回这个重复的数据 我怎样才能避开这件事 from selenium import webdriver class GigsInScotlandMain(InitSpider): name = '

我有一个scrapy spider,它在一个网站上爬行,通过页面上的javascript重新加载内容。为了移动到下一个页面进行抓取,我一直在使用Selenium单击站点顶部的month链接

问题是,即使我的代码按预期在每个链接中移动,爬行器也只会根据月份数刮取第一个月(9月)的数据,并返回这个重复的数据

我怎样才能避开这件事

from selenium import webdriver

class GigsInScotlandMain(InitSpider):
        name = 'gigsinscotlandmain'
        allowed_domains = ["gigsinscotland.com"]
        start_urls = ["http://www.gigsinscotland.com"]


    def __init__(self):
        InitSpider.__init__(self)
        self.br = webdriver.Firefox()

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        self.br.get(response.url)
        time.sleep(2.5)
        # Get the string for each month on the page.
        months = hxs.select("//ul[@id='gigsMonths']/li/a/text()").extract()

        for month in months:
            link = self.br.find_element_by_link_text(month)
            link.click()
            time.sleep(5)

            # Get all the divs containing info to be scraped.
            listitems = hxs.select("//div[@class='listItem']")
            for listitem in listitems:
                item = GigsInScotlandMainItem()
                item['artist'] = listitem.select("div[contains(@class, 'artistBlock')]/div[@class='artistdiv']/span[@class='artistname']/a/text()").extract()
                #
                # Get other data ...
                #
                yield item

问题是您正在重用为初始响应定义的
HtmlXPathSelector
。从selenium浏览器重新定义它
源代码

...
for month in months:
    link = self.br.find_element_by_link_text(month)
    link.click()
    time.sleep(5)

    hxs = HtmlXPathSelector(self.br.page_source)

    # Get all the divs containing info to be scraped.
    listitems = hxs.select("//div[@class='listItem']")
...

非常有用。谢谢